another c pointer question
Code:
char *c = malloc(255);
if (c != NULL)
{
strcpy(c, "Hello world!");
}
void *ptr = c;
char *c2 = ptr;It appears to work, but is it safe? Does the void pointer throw out information pertaining to the number of elements pointed to, or does it simply overlook it, thus allowing c2 access to it(the number of elements) once again?
Pointers don't know anything about sizes of the things that they point to. That's why you can put a pointer into the middle of an array if you want.
What's really happening here is that C strings are null terminated strings. This means that the string "the" is actually 4 characters long. -> 't', 'h', 'e' followed by a zero. To find the length of a string, you actually have to check each character until you find a zero in it.
What's really happening here is that C strings are null terminated strings. This means that the string "the" is actually 4 characters long. -> 't', 'h', 'e' followed by a zero. To find the length of a string, you actually have to check each character until you find a zero in it.
Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Thanks, I always get so confused when dealing with c strings.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Pascal language (pointer) problem | sealfin | 3 | 2,488 |
Nov 27, 2012 04:38 AM Last Post: sealfin |
|
| Member function pointer | LongJumper | 6 | 4,094 |
Oct 31, 2005 05:53 PM Last Post: LongJumper |
|
| Cocoa Method to C Pointer | KiroNeem | 11 | 5,560 |
Sep 12, 2005 10:08 AM Last Post: Zekaric |
|

