Don't understand sizeof char* array

A

Angus

I have this array:
char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
int sz = sizeof(s); // 20 why 20???

I would expect sizeof to report 17. Why is it reporting 20?

to work out the number of elements works ok:
sz = sizeof(char*); // 4 - (size of a pointer)
sz = sizeof(s) / sizeof(char*); // 5 (correct)

I suppose if sizeof(s) had returned 17 then the calculation of the no.
of elements would not be correct. But I would be interested to know
how the array takes up 20 (bytes?)?

Angus
 
I

Ian Collins

I have this array:
char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
int sz = sizeof(s); // 20 why 20???

I would expect sizeof to report 17. Why is it reporting 20?

On your system, sizeof(char*) = 4 and you have 5 in the array.
 
J

Juha Nieminen

Angus said:
I have this array:
char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
int sz = sizeof(s); // 20 why 20???

I would expect sizeof to report 17. Why is it reporting 20?

to work out the number of elements works ok:
sz = sizeof(char*); // 4 - (size of a pointer)
sz = sizeof(s) / sizeof(char*); // 5 (correct)

I suppose if sizeof(s) had returned 17 then the calculation of the no.
of elements would not be correct. But I would be interested to know
how the array takes up 20 (bytes?)?

You have an array of 5 pointers, each pointer taking 4 bytes. What else
do you expect?

(Those strings are somewhere else entirely and do not contribute to
the size of the array. It's just the pointers that point to the strings.)
 
V

Victor Bazarov

I have this array:
char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
int sz = sizeof(s); // 20 why 20???

I would expect sizeof to report 17. Why is it reporting 20?

Why it is reporting 20 you've already found out (can you multiply 4 by
5?) but what is a mystery to me is how you came up with 17? For
instance, sizeof("cat") is 4 (it's an array of four characters that *has
to* include the terminating null character), and sizeof("mouse") is 6
(same principle). Even if you put those characters all in one array:

char s[] = "cat" "cat" "dog" "mouse" "rat";

sizeof(s) will be 18 because it has to add the terminating null character...

And, just for the sake of our children who will be forced to live in the
world where you're going to be programming, don't initialize a pointer
to NON-CONST char with a literal, OK? Your declaration should be

const char* s[] = ...
to work out the number of elements works ok:
sz = sizeof(char*); // 4 - (size of a pointer)
sz = sizeof(s) / sizeof(char*); // 5 (correct)

I suppose if sizeof(s) had returned 17 then the calculation of the no.
of elements would not be correct. But I would be interested to know
how the array takes up 20 (bytes?)?

Array takes up [sizeof(element) x numberofelements] bytes. Since it's
an array of pointers... Well, you have your explanation.

V
 
G

gwowen

I have this array:
    char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
    int sz = sizeof(s);              // 20  why 20???
I would expect sizeof to report 17.  Why is it reporting 20?

Why it is reporting 20 you've already found out (can you multiply 4 by
5?) but what is a mystery to me is how you came up with 17?  

You know perfectly well how he came up with 17. You know why its
wrong, but you know how he came up with it.
 
V

Victor Bazarov

I have this array:
char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
int sz = sizeof(s); // 20 why 20???
I would expect sizeof to report 17. Why is it reporting 20?

Why it is reporting 20 you've already found out (can you multiply 4 by
5?) but what is a mystery to me is how you came up with 17?

You know perfectly well how he came up with 17. You know why its
wrong, but you know how he came up with it.

Shhh! Don't tell him that! You know, give the man a fish...
 
I

Immortal Nephi

On 8/9/2011 5:34 AM, Angus wrote:
I have this array:
     char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
     int sz = sizeof(s);              // 20  why 20???
I would expect sizeof to report 17.  Why is it reporting 20?
Why it is reporting 20 you've already found out (can you multiply 4 by
5?) but what is a mystery to me is how you came up with 17?
You know perfectly well how he came up with 17.  You know why its
wrong, but you know how he came up with it.

Shhh!  Don't tell him that!  You know, give the man a fish...

He does not need to include terminating null character in ALL string
if he does clever design.

Here is—

char* s[] = {"cat", "cat", "dog", "mouse", "rat"};

Try this

char s[][ 3 ] =
{
{ ‘c’, ‘a’, ‘t’ },
{ ‘c’, ‘a’, ‘t’ },
{ ‘d’, ‘o’, ‘g’ },
{ ‘m’, ‘o’, ‘u’ },
{ ‘r’, ‘a’, ‘t’ }
};

You get all 15 characters. Create another variable to support more
than 3 characters like ‘m’, ‘o’, ‘u’ plus ‘s’ and ‘e’. You will get
17 characters.

Class design takes care to store size information. If you want to
convert from this class to char*, you have to include terminating null
character *ALWAYS*.

You can try to do “cout << s” and you will notice that all five words
are printed together into one word without space character before one
terminating null character is included.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top