Reputation: 29896
I am trying to get rid of some confusion when working with C.
Is using char *name
in some sense the same as working with NSString *name
in the sense that the pointer still points to the first memory allocation for it?
Ofcourse NSString
has a bunch of extras but that isnt what I mean, will working with char *name
allow me to work as if it was a NSString *name
, so that I could in the future just work with the pointer "name" ?
Upvotes: 4
Views: 16906
Reputation: 17564
The answer is no.
char*
is meant to point to a simple array of (or single) char
data values.
char* myCharPtr = "This is a string.";
//In memory: myCharPtr contains an address, e.g. |0x27648164|
//at address 0x27648164: |T|h|i|s| |i|s| |a| |s|t|r|i|n|g|.|\0|
On the other hand, NSString *name
will be a pointer to an object which could have lots of extras, and you can't rely on where the actual character data is stored, or how. It is not encoded as ASCII (Sherm Pendley down below said it's UTF-16), and it could have extra data like the string's length, etc.
NSString* myStringPtr = @"This is an NSString.";
//In memory: myStringPtr contains e.g. |0x27648164|
//at address 0x27648164: |Object data|length|You don't know what else|UTF-16 data|etc.|
You alter and access unknown objects through their exposed methods because you don't know how they are formatted in memory, and thus can't access their data directly. (Or because they encapsulate themselves to hide it from you.)
You can still use the NSString
pointer in future if you declare it as NSString *name
though. This is what I mean:
NSString *name = @"This is a string.";
NSString *sameName = name; //This is pointing to the same object as name
NSLog(@"%@", sameName); //This prints "This is a string.", and if NSStrings were mutable, changing it would change name as well.
//They point to the same object.
Upvotes: 22
Reputation: 13612
In a sense - a somewhat non-useful sense, to be sure - they are the same, in that both are pointer types that refer to a memory address. That similarity turns out to be not very useful in practice though. To make use of either one, you have to take into account the type of "thing" they point to, and how one uses an array of char and an NSString instance is entirely different. Even for something as simple as assignment, one needs to handle the NSString instance in accordance to Cocoa's memory-management rules, using either -copy or -retain instead of a simple "foo = bar" assignment.
Upvotes: 0
Reputation: 78825
char*
and NSString*
are two completely different implementations of strings.
NSString
can only be used in Objective C (not in plain C), represents immutable strings, is based on Unicode characters and - being an object-oriented class - provides many methods. Furthermore, they are reference counted and always allocated on the heap.
char*
is just any array of bytes, whose encoding is not well defined, it's mutable and it isn't object-oriented. char
arrays can be allocated on the heap (with malloc
) or on the stack. The former requires a call to free
, while the latter must never be freed.
Upvotes: 5
Reputation: 36082
Is using char *name in some sense the same as working with NSString *name in the sense that the pointer still points to the first memory allocation for it?
I think you should regard the NSString* name as a handle to the object rather than a pointer to the object and as such not assume it points to the first memory allocation. I think that would be a better metaphor.
Upvotes: 2
Reputation: 4423
No,
char * is a pointer to an array of characters. NSString is a pointer to an NSString object. I cannot find any documentation of it, but I believe that NSString boxes a char *. Doing name[1] against NSString *name would refer to the 2nd element of an array of NSString objects.
Upvotes: 1