C Differentiating Between Pointer to Char and Pointer to Char Array -
i'm reading k&r , have gotten bit confused in character pointers section. k&r provides following version of strcpy pointers:
void strcpy(char *s, char *t) { while ( (*s = *t) != '\0') { s++; t++; } } how different using pointer single character? or c trusting know i'm doing , both cases use pointer character? it's when have array of these characters, it's terminated \0, whereas if it's character, subsequent spots in memory anything... right?
how different using pointer single character?
the difference when use pointer single character should not increment pointer once condition must not dereference it. in case of pointer single character of string, pointer can incremented till 1 past last element.
6.5.6 additive operators:
if both pointer operand , result point elements of same array object, or 1 past last element of array object, evaluation shall not produce overflow; [...]
in fact, pointer single element behave pointer array of 1 element.
char c = 'a'; char *p = &c; // pointer char is similar (except c array in case)
char c[1] = {'a'}; char *p = c; // pointer first element of array c. or c trusting know i'm doing , both cases use pointer character?
yes. responsibility should take care pointer should point element of array/string object or 1 past provided no dereferencing in latter case.
Comments
Post a Comment