sizeof and pointer

Discussion in 'C Programming' started by kevin, Jun 14, 2007.

  1. kevin

    kevin Guest

    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?
    kevin, Jun 14, 2007
    #1
    1. Advertising

  2. kevin

    Ian Collins Guest

    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.
    Ian Collins, Jun 14, 2007
    #2
    1. Advertising

  3. kevin

    Chris Dollin Guest

    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
    Chris Dollin, Jun 14, 2007
    #3
  4. 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.
    Richard Heathfield, Jun 14, 2007
    #4
  5. 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.
    Richard Heathfield, Jun 14, 2007
    #5
  6. kevin

    Ian Collins Guest

    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.
    Ian Collins, Jun 14, 2007
    #6
  7. 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.
    Richard Heathfield, Jun 14, 2007
    #7
  8. kevin

    Ian Collins Guest

    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.
    Ian Collins, Jun 14, 2007
    #8
  9. kevin

    kevin Guest

    On 6 14 , 3 36 , 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.
    >
    > --
    > 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.
    kevin, Jun 14, 2007
    #9
  10. kevin

    kevin Guest

    On 6 14 , 3 41 , kevin <> wrote:
    > On 6 14 , 3 36 , 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.

    >
    > > --
    > > 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.
    kevin, Jun 14, 2007
    #10
  11. kevin

    Chris Dollin Guest

    kevin wrote:

    (from his OP)

    this is my programm
    ....
    char *p="abcde";
    char a[]="abcde";

    (end-from)

    .....
    > On 6 14 , 3 41 , kevin <> wrote:


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


    Because the declaration `char *p;` declares `p` as pointer-to-char, while
    the declaration (with initialisation) `char a[] = "abcde";` declares
    `a` as `array[6]char`.

    (And `sizeof` is one of the places where an array doesn't decay into a
    pointer to its first element.)

    --
    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
    Chris Dollin, Jun 14, 2007
    #11
  12. kevin <> writes:
    > 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?


    Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 14, 2007
    #12
  13. kevin

    anil Guest

    On Jun 14, 2:42 pm, Keith Thompson <> wrote:
    > kevin <> writes:
    > > 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?

    >
    > Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
    >
    > --
    > Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    > "We must do something. This is something. Therefore, we must do this."
    > -- Antony Jay and Jonathan Lynn, "Yes Minister"


    pointers and arrays are interchangeables in some scenarios but not in
    all. Array name is treated as 'pointer to first element' in function
    arguments and in expressions, execptions are when array name is passed
    argument to sizeof() and when array name is used with & operator. In
    all other situations arrays are arrays and pointers are pointers.
    anil, Jun 14, 2007
    #13
  14. On Thu, 14 Jun 2007 10:46:59 -0000, in comp.lang.c , anil
    <> wrote:

    >pointers and arrays are interchangeables in some scenarios but not in
    >all. Array name is treated as 'pointer to first element' in function
    >arguments and in expressions, execptions are when array name is passed
    >argument to sizeof()


    This isn't actually an exception. sizeof() is not a function, its an
    operator, and in this case the array is the operand.

    >In all other situations arrays are arrays and pointers are pointers.


    Indeed.
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Jun 14, 2007
    #14
  15. anil said:

    <snip>

    > Array name is treated as 'pointer to first element' in function
    > arguments and in expressions


    Function arguments *are* expressions.

    <snip>

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Jun 14, 2007
    #15
  16. kevin

    anil Guest

    On Jun 14, 6:08 pm, Richard Heathfield <> wrote:
    > anil said:
    >
    > <snip>
    >
    > > Array name is treated as 'pointer to first element' in function
    > > arguments and in expressions

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


    Yes function arguments are actually expressions, i used the term
    'expression' and 'function arguments' to distinguish between functions
    and some expression like a=b+c; etc.
    anil, Jun 15, 2007
    #16
  17. anil <> writes:
    > On Jun 14, 6:08 pm, Richard Heathfield <> wrote:
    >> anil said:
    >> <snip>
    >>
    >> > Array name is treated as 'pointer to first element' in function
    >> > arguments and in expressions

    >>
    >> Function arguments *are* expressions.
    >>
    >> <snip>

    [signature snipped]
    >
    > Yes function arguments are actually expressions, i used the term
    > 'expression' and 'function arguments' to distinguish between functions
    > and some expression like a=b+c; etc.


    But in this case, there is no meaningful distinction. There's nothing
    special about function arguments as far as array conversion is
    concerned.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 15, 2007
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,305
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    616
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,391
    Lawrence Kirby
    May 12, 2005
  4. Alex Vinokur
    Replies:
    7
    Views:
    489
    Clark S. Cox III
    Aug 14, 2006
  5. Alex Vinokur

    sizeof (size_t) and sizeof (pointer)

    Alex Vinokur, Nov 12, 2007, in forum: C++
    Replies:
    19
    Views:
    780
    Ben Rudiak-Gould
    Nov 30, 2007
Loading...

Share This Page