0

Working on some pointer related things in C.I'm wondering what the third line is doing?

char *return_pointer;
static char string_buffer[MAX_WORD_SIZE];
return_pointer = &string_buffer[sizeof(string_buffer)-1]; 
*return_pointer = '\0';
palanisr
  • 11
  • 2
  • 2
    What is it that you don't understand in that line? – risingDarkness Dec 14 '15 at 16:43
  • 1
    It seems to be used to terminate the string at the last char in the buffer. – Paul R Dec 14 '15 at 16:44
  • 1
    Maybe `return_pointer` is needed for something else later, but as the code snippet stands, the last two lines could be accomplished by: `string_buffer[sizeof(string_buffer)-1] = '\0';`. Maybe that helps clarify. In either case, they're making sure that the last byte of the buffer is a NULL (`'\0'`). – lurker Dec 14 '15 at 16:46
  • Could also be there for readability I guess. – Pablo Jomer Dec 14 '15 at 17:51

1 Answers1

4

The statement

return_pointer = &string_buffer[sizeof(string_buffer)-1];   

is assigning the address of last element of string_buffer to return_pointer.
The statement

*return_pointer = '\0';  

is simply terminating the string_buffer with null character.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • additionally if i use *--ptr_return=*message++; am I incrementing the address then using the pointer. – palanisr Dec 18 '15 at 12:20
  • You need to [read this](http://stackoverflow.com/questions/859770/on-a-dereferenced-pointer-in-c?lq=1_). – haccks Dec 18 '15 at 12:30