0

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?
2
  • 1
    Pixie gave the correct answer, but note that "AQ" is a three-byte literal. Commented Apr 3, 2011 at 19:45
  • 2
    Changing it to char cmds[][3] = { ... }; would fix the issue. Commented Apr 3, 2011 at 19:54

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.