eat_a_lemon
eat_a_lemon

Reputation: 3208

C array initialization size in bytes

I expected the size of the following array initialization to be 32. 1 byte characters, 2 bytes for each item in the list, 16 items....= 32. However it is 128 bytes. Why?

char* cmds[] = {"AQ", "BD", "LS", "TW", "AS", "CP", "TR", "CO", "BF", "MS", "SR", "TL", "WT", "PM", "TE", "TC"};
printf("%li\n", sizeof(cmds));
//result is 128
//size of list is 16
//8 bytes per item in the list
//why?

Upvotes: 0

Views: 1266

Answers (3)

Adrian Marinica
Adrian Marinica

Reputation: 2201

Also, if the array wouldn't be one of pointers but of normal chars, since you don't have one character, but multiple per element, then each element would hold three chars, including the NULL at the end. So you would have 16*3=48 bytes.

Upvotes: 1

korona
korona

Reputation: 2229

You've got an array of pointers to strings and the architecture you're compiling on has a 8-byte pointer size. 8 bytes times 16 pointers equals 128 bytes.

Upvotes: 2

Michael
Michael

Reputation: 54705

That's because you have an array of pointers to char. Every pointer is 8-byte (on x64), so 16 pointers x 8 bytes = 128 bytes.

Upvotes: 8

Related Questions