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-25 19:36

An example of when an array name is NOT considered to be of pointer type is when using the sizeof operator:

int main(void)
{
char arr[]="foobar";
printf("%d", sizeof(arr));
return 0;
}

will output

7

as the array has been initialized with 7 char elements (6 lowercase letters + a terminating '\0').

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