How would you read this in English? My concern is with the pointer. Is that pointer associated with char or with string?
Thanks in advance!
it's a pointer to char parameter named string.
So:
char * is the type of the parameter following it
string is the name of the parameter (and you should refer to this one in method body)
The part in parentheses describes the type of the parameter immediately following it—in this case, a pointer to some chars.
string is just the name of the parameter.
The type of the parameter is a pointer to a char.
The parameter is a C string, which is also named string.
[obj initWithName: "whatever"];
C strings are a '\0' terminated sequence of chars, and are declared as char *.
char *foo = "a C string";
NSString *bar = #"an objc string";
Related
I understand pointers work with addresses and not the data itself. This is why I need to use the address-of (&) operator below as I need to assign the address of num to the pointer and not the actual value of num (40).
int num = 40;
int *numPtr = #
Therefore i'm confused as to why I can do this.
NSString *str = #"hello";
I've created a pointer str but instead of giving it an address i'm able to assign it some data, a literal string.
I thought pointers could only hold memory addresses so why am I able to directly assign it some data?
For someone trying to get their head around pointers and objects this is very confusing.
No you are not assigning a literal string to it, # makes a NSString object with the string value hello.
In most C languages strings are just an array of char, where char is a primitive type like int like in your example.
There is a reason you put an # before string literals (when you want an NSString and not a C string) in objective-c
#"String" is basically equivalent to [NSString stringWithCString:"string"] which returns a pointer to an NSString object containing the value "string"
It is the same way 1 is a c type integer, but #1 is a NSNumber representing the value of 1. If you see an # it means "this is shorthand for creating an object". (#[] for NSArrays, #{} for NSDictionarys, #(), #123, #YES, #NO for NSNumbers, and #"" for NSString)
C does not have strings. Usually char arrays are used to represent them.
NSString *str = #"hello";
can be thought of as short hand (literal) for:
char charArray[] = "hello";
NSString *str = [[NSString alloc] initWithBytes:charArray length:sizeof(charArray) encoding:NSUTF8StringEncoding]; // disregard character encoding for this example
or
unichar bla[] = {'h', 'e', 'l', 'l', 'o'};
str = [[NSString alloc] initWithCharacters:bla length:sizeof(bla)];
So an object is created and thus you need a pointer.
Is there any easy way to convert an Objective-C holding class of NSStrings into parameters for a function accepting a variable list of char *? Specifically I have a function like:
-(void)someFunction:(NSSomething *) var
that I want to forward to a C function like
void someCFunction(char * var, ...)
Is there an easy way to go about this?
No, you can only do what you want if the number of arguments you're passing is known at compile time. If you just want to convert a single string, use the -UTF8String message:
// Example with two strings
NSString *str1 = ...;
NSString *str2 = ...;
someCFunction([str1 UTF8String], [str2 UTF8String]); // etc.
But if the number of strings will vary at runtime, you'll need to use a different API, if one is available. For example, if there's an API that took an array of strings, you could convert the Objective-C array into a C array:
// This function takes a variable number of strings. Note: in C/Objective-C
// (but not in C++/Objective-C++), it's not legal to convert 'char **' to
// 'char *const *', so you may sometimes need a cast to call this function
void someCFunction(const char *const *stringArray, int numStrings)
{
...
}
...
// Convert Objective-C array to C array
NSArray *objCArray = ...;
int numStrings = [objCArray count];
char **cStrArray = malloc(numStrings * sizeof(char*));
for (int i = 0; i < count; i++)
cStrArray[i] = [[objCArray objectAtIndex:i] UTF8String];
// Call the function; see comment above for note on cast
someCFunction((const char *const *)cStrArray, numStrings);
// Don't leak memory
free(cStrArray);
This would do the trick:
NSString *string = #"testing string"
const char * p1=[string UTF8String];
char * p2;
p2 = const_cast<char *>(p1);
Yes, this can be done, and is explained here:
How to create a NSString from a format string like #"xxx=%#, yyy=%#" and a NSArray of objects?
And here:
http://www.cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html
With modifications for ARC here:
How to create a NSString from a format string like #"xxx=%#, yyy=%#" and a NSArray of objects?
Also, variable arguments are not statically or strongly typed, as the other poster seems to be suggesting. In fact, there is no clear indication in the callee of how many arguments you really have. Determining the number of arguments generally breaks down into having to either specify the number by an count parameter, using a null terminator, or inferring it from a format string a la (s)print* . This is frankly why the C (s)print* family of functions has been the source of many errors, now made much much safer by the XCode / Clang / GCC compiler that now warns.
As an aside, you can approach statically typed variable arguments in C++ by creating a template method that accepts an array of an unspecified size. This is generally considered bad form though as the compiler generates separate instances for each size of array seen by by the compiler (template bloat).
How to get input from an NSString as scanf ("%#", &str); doesn't work?
scanf will read into a C string and not into a NSString (as far as I know). So, to do what you're trying to do you need to first read your input into a C string (i.e. str) and then make that into an NString as follows
myString = [NSString stringWithUTF8String:str];
By the way, you don't need to pass the address of str i.e. &str if str is an array. Simply do:
scanf("%s",str);
i am a beginer in objective c.i found the following line in code and is not able to understand what it does it do, as storeselect has not been used anywhere in the code.
NSString *storeSelect=#"";
Objective-C builds on C language. In C, quotes are placed around string literals, i.e. "hello". To distinguish NSString and C strings (char pointers, char *), Objective-C uses # in front of strings, so #"" is simply empty NSString. If there was no #, it would be empty C string, e.g. char *myString = "hello world";.
storeSelect is the name of a variable whose type is NSString *, with the value assigned to #""
It's just assigning an empty string to a variable named storeSelect. The #"" is for constant strings.
NSString *storeSelect=#"Hello World";
is a shortcut of -
NSString *str = [NSString stringWithCString:"Hello World"];
as "stringWithCString" is convenience method it will be automatically adds autoreleased.
The below code generates the incompatible pointer type error:
char *PLURAL(int objects, NSString *singluar, NSString *pluralised) {
return objects ==1 ? singluar:pluralised;}
I am new to objective-C and programming in general so can some one help me with this error?
An NSString * is not the same as a char * (or "C-string" in Objective C terminology). You can't convert a pointer from one to the other implicitly like that. You'll have to use a method like cStringUsingEncoding. Also, NSString is immutable, so you'll have to return a const char *.
Alternatively, you could simply return the NSString * instead of char *.
Change the return value to NSString* and you should be fine. You are specifying a return value of char* but actually returning NSString*.
Change it to:
NSString *PLURAL(int objects, NSString *singluar, NSString *pluralised) {
return objects ==1 ? singluar:pluralised;
}
char * is not NSString !