Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Pointers in C

Name: Anonymous 2013-06-25 17:59

Hey guys I'm having a little trouble with the following example

#include <stdio.h>
int main(void)
{
char s[] = "Melbourne City";
printf("%s\n", &s[2]);
return 0;
}

which prints "lbourne City" instead of the address of letter 'l'.
I do understand that the %s in the printf continues until a null terminator is hit, but not why &s[2] returns a character in the first place.
Thanks.

Name: Anonymous 2013-06-26 4:14

>>21
Yeah... if you really want to be pedantic, you may go ahead and cast an address to (int) to printf it via %i or %d - still, (sizeof(int) == sizeof(void *)), so it will work anyway, showing the address in decimal (while %p will show it in hex).

Also, size_t is a datatype for sizes proposed by ANSI C, but it's not really necessary to use it afaik.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List