help:question about sizeof?

Discussion in 'C Programming' started by guoliang, Apr 13, 2007.

  1. guoliang

    guoliang Guest

    help:

    i try the code:
    "
    char str[]={'h','e','l','l','o','\n','\0'};
    char *pstr=str;
    printf("sizeof=%d\n",sizeof(str));
    printf("sizeof=%d\n",sizeof(pstr)); //i want to get sizeof(str) not
    sizeof(void *)

    "
    how can i use pstr to get sizeof string which it point?

    thx!
     
    guoliang, Apr 13, 2007
    #1
    1. Advertising

  2. "guoliang" <> wrote:
    > help:
    >
    > i try the code:
    > "
    > char str[]={'h','e','l','l','o','\n','\0'};


    char str[] = "Hello\n";

    > char *pstr=str;
    > printf("sizeof=%d\n",sizeof(str));


    %d will tell printf to expect an int. You supply a size_t
    which is unlikely to be a type that promotes to int.

    printf("sizeof = %u\n", (int) sizeof(str));

    > printf("sizeof=%d\n",sizeof(pstr)); //i want to get
    > sizeof(str) not sizeof(void *)


    I suspect you meant sizeof(char *), although void and character
    pointers will have the same size.

    > "
    > how can i use pstr to get sizeof string which it point?


    You can't.

    http://c-faq.com/malloc/sizeof.html

    --
    Peter
     
    Peter Nilsson, Apr 13, 2007
    #2
    1. Advertising

  3. guoliang

    seni.yin Guest

    fuction "strlen()"
     
    seni.yin, Apr 13, 2007
    #3
  4. guoliang wrote:
    > help:
    >
    > i try the code:
    > "
    > char str[]={'h','e','l','l','o','\n','\0'};
    > char *pstr=str;
    > printf("sizeof=%d\n",sizeof(str));
    > printf("sizeof=%d\n",sizeof(pstr)); //i want to get sizeof(str) not
    > sizeof(void *)
    >
    > "
    > how can i use pstr to get sizeof string which it point?


    Well, the _string_ has a size of strlen(pstr)+1. You cannot use pstr to
    get the size of the _array_ to which it points: such information is not
    incorporated into the pointer or into the array. In addition to the
    obvious, that the sizeof a pointer is just the sizeof a pointer,
    remember that sizeof yields a compile-time constant. Since the sizes of
    arrays pointed can change during execution, they can hardly be
    compile-time constants.
     
    Martin Ambuhl, Apr 13, 2007
    #4
  5. guoliang

    shaanxxx Guest

    On Apr 13, 10:24 am, Martin Ambuhl <> wrote:
    > guoliang wrote:
    > > help:

    >
    > > i try the code:
    > > "
    > > char str[]={'h','e','l','l','o','\n','\0'};
    > > char *pstr=str;
    > > printf("sizeof=%d\n",sizeof(str));
    > > printf("sizeof=%d\n",sizeof(pstr)); //i want to get sizeof(str) not
    > > sizeof(void *)

    >
    > > "
    > > how can i use pstr to get sizeof string which it point?

    >
    > Well, the _string_ has a size of strlen(pstr)+1. You cannot use pstr to
    > get the size of the _array_ to which it points: such information is not
    > incorporated into the pointer or into the array. In addition to the
    > obvious, that the sizeof a pointer is just the sizeof a pointer,
    > remember that sizeof yields a compile-time constant.

    above statement hold true for c89. c99 has variable length array and
    it needs run sizeof operator for variable length array.

    6.5.3.4 The sizeof operator:
    EXAMPLE 3 In this example, the size of a variable length array is
    computed and returned from a
    function:
    #include <stddef.h>
    size_t fsize3(int n)
    {
    char b[n+3]; // variable length array
    return sizeof b; // execution time sizeof
    }


    > Since the sizes of
    > arrays pointed can change during execution, they can hardly be
    > compile-time constants.
     
    shaanxxx, Apr 13, 2007
    #5
  6. guoliang

    shaanxxx Guest

    On Apr 13, 10:24 am, Martin Ambuhl <> wrote:
    > guoliang wrote:
    > > help:

    >
    > > i try the code:
    > > "
    > > char str[]={'h','e','l','l','o','\n','\0'};
    > > char *pstr=str;
    > > printf("sizeof=%d\n",sizeof(str));
    > > printf("sizeof=%d\n",sizeof(pstr)); //i want to get sizeof(str) not
    > > sizeof(void *)

    >
    > > "
    > > how can i use pstr to get sizeof string which it point?

    >
    > Well, the _string_ has a size of strlen(pstr)+1. You cannot use pstr to
    > get the size of the _array_ to which it points: such information is not
    > incorporated into the pointer or into the array. In addition to the
    > obvious, that the sizeof a pointer is just the sizeof a pointer,
    > remember that sizeof yields a compile-time constant.


    above statement hold true for c89. c99 has variable length array and
    it needs runtime sizeof operator for variable length array.

    6.5.3.4 The sizeof operator:
    EXAMPLE 3 In this example, the size of a variable length array is
    computed and returned from a
    function:
    #include <stddef.h>
    size_t fsize3(int n)
    {
    char b[n+3]; // variable length array
    return sizeof b; // execution time sizeof

    > Since the sizes of
    > arrays pointed can change during execution, they can hardly be
    > compile-time constants.
     
    shaanxxx, Apr 13, 2007
    #6
  7. guoliang

    JimS Guest

    On 12 Apr 2007 20:28:57 -0700, "Peter Nilsson" <>
    wrote:

    >%d will tell printf to expect an int. You supply a size_t
    >which is unlikely to be a type that promotes to int.


    Agree.

    > printf("sizeof = %u\n", (int) sizeof(str));


    Are you sure about that? %u is for unsigned int.

    Jim
     
    JimS, Apr 13, 2007
    #7
  8. On Apr 13, 4:28 pm, JimS <> wrote:
    > On 12 Apr 2007 20:28:57 -0700, "Peter Nilsson" <>
    > > printf("sizeof = %u\n", (int) sizeof(str));

    >
    > Are you sure about that? %u is for unsigned int.


    True. The cast should have been (unsigned). Thanks.

    --
    Peter
     
    Peter Nilsson, Apr 15, 2007
    #8
    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,421
    Ron Natalie
    Oct 14, 2004
  2. Trevor

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

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    664
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,506
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    580
    Joe Smith
    May 22, 2006
  5. Alex Vinokur
    Replies:
    7
    Views:
    515
    Clark S. Cox III
    Aug 14, 2006
Loading...

Share This Page