What does sizeof (char *) do?

I was reading C ++ Primer and this piece of code came up and I was wondering what sizeof (char *) does and why is it so significant?

 char *words[] = {"stately", "plump", "buck", "mulligan"};

 // calculate how many elements in words
 size_t words_size = sizeof(words)/sizeof(char *);

 // use entire array to initialize words2
 list<string> words2(words, words + words_size);

      

Thanks in advance.

+2


source to share


2 answers


Because you would otherwise end up with the number of bytes the word array takes, not the number of elements (char pointers are 4 or 8 bytes on Intel architectures)



+8


source


sizeof(char*)

returns the size of the system pointer. sizeof (words) returns the number of bytes in the array. Since each element of the array is sizeof(char*)

large, the number of elements is equal to number_of_bytes / bytes_per_element, therefore sizeof(words)/sizeof(char*)

.



+6


source







All Articles