Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Don't understand sizeof char* array (http://www.velocityreviews.com/forums/t752616-dont-understand-sizeof-char-array.html)

 Angus 08-09-2011 09:34 AM

Don't understand sizeof char* array

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

 Ian Collins 08-09-2011 09:41 AM

Re: Don't understand sizeof char* array

On 08/ 9/11 09:34 PM, 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?

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

--
Ian Collins

 Juha Nieminen 08-09-2011 11:30 AM

Re: Don't understand sizeof char* array

Angus <anguscomber@gmail.com> 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?
>
> 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.)

 Victor Bazarov 08-09-2011 12:48 PM

Re: Don't understand sizeof char* array

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? 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
--
I do not respond to top-posted replies, please don't ask

 gwowen 08-10-2011 08:23 AM

Re: Don't understand sizeof char* array

On Aug 9, 1:48*pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> 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.

 Victor Bazarov 08-10-2011 01:00 PM

Re: Don't understand sizeof char* array

On 8/10/2011 4:23 AM, gwowen wrote:
> On Aug 9, 1:48 pm, Victor Bazarov<v.baza...@comcast.invalid> wrote:
>> 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...

 Immortal Nephi 08-11-2011 01:17 AM

Re: Don't understand sizeof char* array

On Aug 10, 8:00*am, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> On 8/10/2011 4:23 AM, gwowen wrote:
>
> > On Aug 9, 1:48 pm, Victor Bazarov<v.baza...@comcast.invalid> *wrote:
> >> 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.

 All times are GMT. The time now is 05:29 AM.