Please clear this doubt :(

int main()
{
char *b = "Hello";
printf("%c\n",*b);
printf("Only b is %d\n",sizeof(b));
printf("Pointer b is %d\n",sizeof(*b));
}

The output of above program is :-

H
Only b is 4
Pointer b is 1

Why so? Please Help...
Why what exactly? Were you expecting some other result?
1
2
3
4
5
6
7
int main()
{
    char *b = "Hello";
    printf("%c\n",*b);
    printf("Only b is %d\n",sizeof(b));
    printf("Pointer b is %d\n",sizeof(*b));
}

Line 3 defines b as a pointer to a string.
Line 4 uses the %c code to output the first character of that string.
Line 5 outputs the size of the pointer.
Line 6 outputs the size of the char.



Another example:
1
2
3
4
    char a[] = "World!";
    printf("%s\n", a);
    printf("Array a size %d\n", sizeof(a));
    printf("String a size %d\n", strlen(a));

Output:
World!
Array a size 7
String a size 6


Last edited on
Topic archived. No new replies allowed.