0
#include<stdio.h>

int main(int argc, char *argv[])
{
    char *states[] = {
        "California", "Oregon",
        "Washington", "Texas"
    };

    printf("size of states: %d",sizeof(states));

    return 0;
}

the result of this is:
size of states: 16

why is that?I know that an int is 4 bytes,while a char is 1 byte.I got confused why it's 16 and not 4.

2
Contributors
1
Reply
15
Views
3 Years
Discussion Span
Last Post by sepp2k
0

sizeof(states) gives you the number of bytes in the array. The number of bytes in an array equals the number of elements times the number of bytes per element. The elements in states are char pointers and apparently pointers have 4 bytes on your platform (so you're running a 32-bit system or at least a 32-bit compiler), so we get 4 * 4 = 16 bytes total.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.