Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Don't understand sizeof char* array

Reply
Thread Tools

Don't understand sizeof char* array

 
 
Angus
Guest
Posts: n/a
 
      08-09-2011
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



 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-09-2011
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
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      08-09-2011
Angus <(E-Mail Removed)> 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.)
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-09-2011
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
 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      08-10-2011
On Aug 9, 1:48*pm, Victor Bazarov <(E-Mail Removed)> 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.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-10-2011
On 8/10/2011 4:23 AM, gwowen wrote:
> On Aug 9, 1:48 pm, Victor Bazarov<(E-Mail Removed)> 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...
 
Reply With Quote
 
Immortal Nephi
Guest
Posts: n/a
 
      08-11-2011
On Aug 10, 8:00*am, Victor Bazarov <(E-Mail Removed)> wrote:
> On 8/10/2011 4:23 AM, gwowen wrote:
>
> > On Aug 9, 1:48 pm, Victor Bazarov<(E-Mail Removed)> *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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? m.labanowicz@gmail.com C Programming 46 01-14-2013 10:50 PM
sizeof( int ) != sizeof( void * ) blufox C Programming 2 05-22-2006 03:25 PM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments