Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sizeof and pointer

Reply
Thread Tools

sizeof and pointer

 
 
kevin
Guest
Posts: n/a
 
      06-14-2007
this is my programm
....
char *p="abcde";
char a[]="abcde";
.....

printf("the size of p is:%d\n",sizeof(p));
printf("the size of a is:%d\n",sizeof(a));
.....
---------------------------------------------------------
and the output is:

the size of p is:1
the size of a is:6


------------------------
anyone can tell me why?

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-14-2007
kevin wrote:
> this is my programm
> ....
> char *p="abcde";
> char a[]="abcde";
> .....
>
> printf("the size of p is:%d\n",sizeof(p));
> printf("the size of a is:%d\n",sizeof(a));
> .....
> ---------------------------------------------------------
> and the output is:
>
> the size of p is:1
> the size of a is:6
>
> ------------------------
> anyone can tell me why?
>

Are you sure? There aren't many systems around (at least not hosted
ones!) with an 8 bit char*.

p is a char* and a is any array of 6 char, sizeof(char) is 1, so
sizeof(char[6]) is 6.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      06-14-2007
kevin wrote:

> this is my programm
> ...
> char *p="abcde";
> char a[]="abcde";
> ....
>
> printf("the size of p is:%d\n",sizeof(p));
> printf("the size of a is:%d\n",sizeof(a));
> ....
> ---------------------------------------------------------
> and the output is:
>
> the size of p is:1
> the size of a is:6
>
>
> ------------------------
> anyone can tell me why?


I don't think the code you posted is the code you ran, unless your
platform is one where sizeof(char)==sizeof(char*), which seems unlikely
but possible.

The literal answer -- assuming that your (incomplete) code is really
what you ran -- is "the size of a pointer-to-char is 1, the size of the
array `a` is 6 (six chars, abcde plus the nul at the end)".

--
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-14-2007
kevin said:

> this is my programm
> ...
> char *p="abcde";
> char a[]="abcde";
> ....
>
> printf("the size of p is:%d\n",sizeof(p));
> printf("the size of a is:%d\n",sizeof(a));
> ....
> ---------------------------------------------------------
> and the output is:
>
> the size of p is:1
> the size of a is:6
>
>
> ------------------------
> anyone can tell me why?


First, let's write a correct program:

#include <stdio.h> /* a prototype for printf is *REQUIRED* */

int main(void) /* executable code needs a function in which to live */
{
char *p = "abcde";
char a[] = "abcde";

printf("the size of p is %d\n", (int)sizeof p);
printf("the size of a is %d\n", (int)sizeof a);

return 0;
}

If you are getting 1 for p, you have a rather unusual system, in which
bytes are so wide - probably 16 bits or more - that you can fit a
pointer into one. What is more likely is that you typed your question
instead of copying it from your real program.

If your question is simply "why aren't sizeof p and sizeof a the same?",
that's easy. Arrays are not pointers. Pointers are not arrays. Arrays
can be very large indeed, but pointers are generally quite small
(although rarely as small as 1, I must admit). Arrays are cities.
Pointers are signposts. It is possible to imagine a city as small as a
signpost, of course, but one doesn't normally bother.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-14-2007
Ian Collins said:

> kevin wrote:


<snip>
>>
>> the size of p is:1
>> the size of a is:6
>>
>> ------------------------
>> anyone can tell me why?
>>

> Are you sure? There aren't many systems around (at least not hosted
> ones!) with an 8 bit char*.


There are, however, a few systems around with 16- or even 32-bit char,
so that a pointer value might easily fit in a byte.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-14-2007
Richard Heathfield wrote:
> Ian Collins said:
>
>> kevin wrote:

>
> <snip>
>>> the size of p is:1
>>> the size of a is:6
>>>
>>> ------------------------
>>> anyone can tell me why?
>>>

>> Are you sure? There aren't many systems around (at least not hosted
>> ones!) with an 8 bit char*.

>
> There are, however, a few systems around with 16- or even 32-bit char,
> so that a pointer value might easily fit in a byte.
>

True, but as you said in your other post, unlikely.

--
Ian Collins.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-14-2007
Ian Collins said:

> Richard Heathfield wrote:
>> Ian Collins said:
>>
>>> kevin wrote:

>>
>> <snip>
>>>> the size of p is:1
>>>> the size of a is:6
>>>>
>>>> ------------------------
>>>> anyone can tell me why?
>>>>
>>> Are you sure? There aren't many systems around (at least not hosted
>>> ones!) with an 8 bit char*.

>>
>> There are, however, a few systems around with 16- or even 32-bit
>> char, so that a pointer value might easily fit in a byte.
>>

> True, but as you said in your other post, unlikely.


Yes, but my point in /this/ reply was to stress the fact that char
needn't be 8 bits, so a sizeof(char *) of 1 needn't mean an 8-bit
char *, which you did appear to have assumed.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-14-2007
Richard Heathfield wrote:
> Ian Collins said:
>
>> Richard Heathfield wrote:
>>> Ian Collins said:
>>>
>>>> kevin wrote:
>>> <snip>
>>>>> the size of p is:1
>>>>> the size of a is:6
>>>>>
>>>>> ------------------------
>>>>> anyone can tell me why?
>>>>>
>>>> Are you sure? There aren't many systems around (at least not hosted
>>>> ones!) with an 8 bit char*.
>>> There are, however, a few systems around with 16- or even 32-bit
>>> char, so that a pointer value might easily fit in a byte.
>>>

>> True, but as you said in your other post, unlikely.

>
> Yes, but my point in /this/ reply was to stress the fact that char
> needn't be 8 bits, so a sizeof(char *) of 1 needn't mean an 8-bit
> char *, which you did appear to have assumed.
>

Fair point. I should have said "There aren't many systems around (at
least not hosted ones) where sizeof(char*) == sizeof(char)".

--
Ian Collins.
 
Reply With Quote
 
kevin
Guest
Posts: n/a
 
      06-14-2007
On 6 14 , 3 36 , Richard Heathfield <(E-Mail Removed)> wrote:
> Ian Collins said:
>
>
>
>
>
> > Richard Heathfield wrote:
> >> Ian Collins said:

>
> >>> kevin wrote:

>
> >> <snip>
> >>>> the size of p is:1
> >>>> the size of a is:6

>
> >>>> ------------------------
> >>>> anyone can tell me why?

>
> >>> Are you sure? There aren't many systems around (at least not hosted
> >>> ones!) with an 8 bit char*.

>
> >> There are, however, a few systems around with 16- or even 32-bit
> >> char, so that a pointer value might easily fit in a byte.

>
> > True, but as you said in your other post, unlikely.

>
> Yes, but my point in /this/ reply was to stress the fact that char
> needn't be 8 bits, so a sizeof(char *) of 1 needn't mean an 8-bit
> char *, which you did appear to have assumed.
>
> --
> Richard Heathfield
> "Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
> email: rjh at the above domain, - www.- -
>
> - -



i'm sorry, all is my fault,i have run the code on my coputer certainly
not 8 bit one even exit
,but i wrote a wrong result while post the topic.

 
Reply With Quote
 
kevin
Guest
Posts: n/a
 
      06-14-2007
On 6 14 , 3 41 , kevin <(E-Mail Removed)> wrote:
> On 6 14 , 3 36 , Richard Heathfield <(E-Mail Removed)> wrote:
>
>
>
>
>
> > Ian Collins said:

>
> > > Richard Heathfield wrote:
> > >> Ian Collins said:

>
> > >>> kevin wrote:

>
> > >> <snip>
> > >>>> the size of p is:1
> > >>>> the size of a is:6

>
> > >>>> ------------------------
> > >>>> anyone can tell me why?

>
> > >>> Are you sure? There aren't many systems around (at least not hosted
> > >>> ones!) with an 8 bit char*.

>
> > >> There are, however, a few systems around with 16- or even 32-bit
> > >> char, so that a pointer value might easily fit in a byte.

>
> > > True, but as you said in your other post, unlikely.

>
> > Yes, but my point in /this/ reply was to stress the fact that char
> > needn't be 8 bits, so a sizeof(char *) of 1 needn't mean an 8-bit
> > char *, which you did appear to have assumed.

>
> > --
> > Richard Heathfield
> > "Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
> > email: rjh at the above domain, - www.- -

>
> > - -

>
> i'm sorry, all is my fault,i have run the code on my coputer certainly
> not 8 bit one even exit
> ,but i wrote a wrong result while post the topic.- -
>
> - -


but i also want to know the p standfor an address,while the a standfor
the whole array.

 
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
sizeof (size_t) and sizeof (pointer) Alex Vinokur C++ 19 11-30-2007 11:11 PM
sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) == 1?) Alex Vinokur C Programming 7 08-14-2006 04:57 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