Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does sizeof(char *) do?

Tags:

c++

I was reading through the c++ Primer and this code snippet came up and I was wondering what does the sizeof(char *) do 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.

like image 348
Chris Bui Avatar asked Jul 10 '26 12:07

Chris Bui


1 Answers

Because otherwise you would get the number of bytes that words array takes up, not the number of elements (char pointers are either 4 or 8 bytes on Intel architectures)

like image 79
Ana Betts Avatar answered Jul 12 '26 02:07

Ana Betts