I'm having some trouble wrapping my head around this simple dereferencing statement.
I've tried printing **names, and then I get what I expect to get from *names -- 'C'. However, *names gives me 'D'.
#include <stdio.h>
int main(void)
{
char *names[] = {"Carl", "David", "Gibson", "Long", "Paul"};
printf("%c\n", *names);
return 0;
}
The console prints out 'D'. I'm not sure why the resulting char from *names is not the first letter of the first item, 'C'.
printf("%s\n", *names);orprintf("%c\n", **names);%cformat specifier expects a single character (an integer argument toprintf). The%sformat specifier expects a string (achar *). You're passing achar *to a%cspecifier, which makes no sense. Your compiler should have warned about this (make sure your code is clear of warnings before posting here, unless you truly don't understand the warning, in which case that should be the title of your post).printf("%c\n", (*(names[1]+(sizeof(char)*2))));where the1represents the second name in the array, and the2represents the letter within the name. This would outputvfromDavid. Or alternatively, just use%s;).sizeofout, pointer arithmetic already scales by the element size. The only reason it isn't blowing up in your face is thatsizeof(char)is guaranteed to be 1.