sizeof struct array

Discussion in 'C Programming' started by ak, Jul 17, 2004.

  1. ak

    ak Guest

    struct xy{
    int x;
    int y;
    }
    _xy[32];

    size_of_xy(struct xy * a) {
    int len = sizeof a;
    printf( "\sizeof xy: %i\n", len );
    }

    int main( int argc, char ** argv ) {
    size_of_xy(_xy);
    }

    the problem is that size_of_xy prints size of xy struct.
    I need to print size of _xy array.

    any ideas?

    Andrei
     
    ak, Jul 17, 2004
    #1
    1. Advertising

  2. ak

    S.Tobias Guest

    ak <> wrote:
    > struct xy{
    > int x;
    > int y;
    > }
    > _xy[32];


    > size_of_xy(struct xy * a) {
    > int len = sizeof a;


    ITYM sizeof *a;
    The result of `sizeof' is type `size_t'.

    > printf( "\sizeof xy: %i\n", len );
    > }


    > int main( int argc, char ** argv ) {
    > size_of_xy(_xy);
    > }


    > the problem is that size_of_xy prints size of xy struct.
    > I need to print size of _xy array.


    No way.

    > any ideas?


    You have to pass the length of the array in another argument.

    If you want more data encapsulation, you might think of sth like:

    struct xy_array {
    struct xy *xy_arr;
    size_t xy_arr_len;
    }; /*you are responsible for its contents*/

    size_of_xy_array(struct xy_array *a) {
    printf("size: %u\n", (unsigned)(sizeof(struct xy) * a->xy_arr_len));
    }

    --
    Stan Tobias
    sed '/[A-Z]//g' to email
     
    S.Tobias, Jul 17, 2004
    #2
    1. Advertising

  3. ak wrote:

    > struct xy{
    > int x;
    > int y;
    > }
    > _xy[32];
    >
    > size_of_xy(struct xy * a) {
    > int len = sizeof a;
    > printf( "\sizeof xy: %i\n", len );
    > }
    >
    > int main( int argc, char ** argv ) {
    > size_of_xy(_xy);
    > }
    >
    > the problem is that size_of_xy prints size of xy struct.
    > I need to print size of _xy array.
    >
    > any ideas?


    /* mha: you could read the FAQ before posting. Or if that's beyond
    you, check the archives at groups.google.com. Those are the things
    civilized people do before posting. In any case, consider the code
    below */
    #include <stdio.h>

    #define array_size_info(a) do \
    printf("The array has size %lu,"\
    " each of the %lu elements has size %lu\n",\
    sizeof a, sizeof a/sizeof *a, sizeof *a);\
    while (0)

    int main(void)
    {
    struct xy
    {
    int x;
    int y;
    }
    xy[32];
    array_size_info(xy);
    return 0;
    }


    [output on my implementation]

    The array has size 256, each of the 32 elements has size 8
     
    Martin Ambuhl, Jul 18, 2004
    #3
  4. ak

    ak Guest

    > /* mha: you could read the FAQ before posting. Or if that's beyond
    > you, check the archives at groups.google.com. Those are the things
    > civilized people do before posting. In any case, consider the code
    > below */
    > #include <stdio.h>
    >
    > #define array_size_info(a) do \
    > printf("The array has size %lu,"\
    > " each of the %lu elements has size %lu\n",\
    > sizeof a, sizeof a/sizeof *a, sizeof *a);\
    > while (0)
    >
    > int main(void)
    > {
    > struct xy
    > {
    > int x;
    > int y;
    > }
    > xy[32];
    > array_size_info(xy);
    > return 0;
    > }
    >
    >
    > [output on my implementation]
    >
    > The array has size 256, each of the 32 elements has size 8


    #include <stdio.h>

    #define array_size_info(a) do \
    printf("The array has size %lu,"\
    " each of the %lu elements has size %lu\n",\
    sizeof a, sizeof a/sizeof *a, sizeof *a);\
    while(0)

    struct xy {
    int x;
    int y;
    }
    first_xy[32], second_xy[64];

    void do_something(struct xy *_xy) {
    array_size_info(_xy);
    }

    int main( void ) {
    array_size_info( first_xy );
    array_size_info( second_xy );
    do_something(first_xy);
    do_something(second_xy);
    return 0;
    }

    the output is:

    The array has size 256, each of the 32 elements has size 8
    The array has size 512, each of the 64 elements has size 8
    The array has size 4, each of the 0 elements has size 8
    The array has size 4, each of the 0 elements has size 8

    as you can see array_size_info works wrong in do_something();

    --
    Andrei Kouznetsov
     
    ak, Jul 18, 2004
    #4
  5. ak wrote:

    >> /* mha: you could read the FAQ before posting. Or if that's beyond
    >> you, check the archives at groups.google.com. Those are the things
    >> civilized people do before posting. In any case, consider the code
    >> below */
    >>#include <stdio.h>
    >>
    >>#define array_size_info(a) do \
    >> printf("The array has size %lu,"\
    >> " each of the %lu elements has size %lu\n",\
    >> sizeof a, sizeof a/sizeof *a, sizeof *a);\
    >> while (0)
    >>
    >>int main(void)
    >>{
    >> struct xy
    >> {
    >> int x;
    >> int y;
    >> }
    >> xy[32];
    >> array_size_info(xy);
    >> return 0;
    >>}
    >>
    >>
    >>[output on my implementation]
    >>
    >>The array has size 256, each of the 32 elements has size 8

    >
    >
    > #include <stdio.h>
    >
    > #define array_size_info(a) do \
    > printf("The array has size %lu,"\
    > " each of the %lu elements has size %lu\n",\
    > sizeof a, sizeof a/sizeof *a, sizeof *a);\
    > while(0)
    >
    > struct xy {
    > int x;
    > int y;
    > }
    > first_xy[32], second_xy[64];
    >
    > void do_something(struct xy *_xy) {
    > array_size_info(_xy);
    > }
    >
    > int main( void ) {
    > array_size_info( first_xy );
    > array_size_info( second_xy );
    > do_something(first_xy);
    > do_something(second_xy);
    > return 0;
    > }
    >
    > the output is:
    >
    > The array has size 256, each of the 32 elements has size 8
    > The array has size 512, each of the 64 elements has size 8
    > The array has size 4, each of the 0 elements has size 8
    > The array has size 4, each of the 0 elements has size 8
    >
    > as you can see array_size_info works wrong in do_something();


    It works fine. You gave it a broken argument. I suggested that you
    should have read the FAQ. I repeat that. If you can't figure out that
    the argument given to array_size_info in do_something is a pointer and
    not an array, may God have mercy on your soul.

    >
     
    Martin Ambuhl, Jul 18, 2004
    #5
  6. ak

    ak Guest

    > If you can't figure out that
    > the argument given to array_size_info in do_something is a pointer and
    > not an array, may God have mercy on your soul.
    >

    sure, he has.

    why I can't get size of array if I have only pointer to it?

    --
    Andrei
     
    ak, Jul 18, 2004
    #6
  7. ak

    Alex Fraser Guest

    "ak" <> wrote in message news:cddd6p$rk5$...
    > > If you can't figure out that
    > > the argument given to array_size_info in do_something is a pointer and
    > > not an array, may God have mercy on your soul.

    >
    > sure, he has.
    >
    > why I can't get size of array if I have only pointer to it?


    Because a pointer to an array holds a value that tells you where the start
    of the array is, and nothing else (such as the size).

    This is why, if you need it, you must pass the size of the array in another
    argument. You might not always need the size; sometimes you can use a
    sentinel value to indicate the end. A good example of this is strings in the
    standard library, where '\0' is used as the sentinel value.

    Alex
     
    Alex Fraser, Jul 18, 2004
    #7
  8. ak wrote:
    >>If you can't figure out that
    >>the argument given to array_size_info in do_something is a pointer and
    >>not an array, may God have mercy on your soul.
    >>

    >
    > sure, he has.
    >
    > why I can't get size of array if I have only pointer to it?


    I suggestes in my first reply, and repeated in the second, that you
    should look to the FAQ, which you have obviously not done. How big a
    hint do you need?
    If you would *READ THE DAMN FAQ* you wouldn't keep asking these stupid
    questions.
     
    Martin Ambuhl, Jul 18, 2004
    #8
  9. ak

    ak Guest

    > I suggestes in my first reply, and repeated in the second, that you
    > should look to the FAQ, which you have obviously not done. How big a
    > hint do you need?
    > If you would *READ THE DAMN FAQ* you wouldn't keep asking these stupid
    > questions.


    Stay cool.
    I'll make it. Sometimes.

    --
    Andrei
     
    ak, Jul 18, 2004
    #9
  10. ak

    Al Bowers Guest

    ak wrote:
    >>I suggestes in my first reply, and repeated in the second, that you
    >>should look to the FAQ, which you have obviously not done. How big a
    >>hint do you need?
    >>If you would *READ THE DAMN FAQ* you wouldn't keep asking these stupid
    >>questions.

    >
    >
    > Stay cool.
    > I'll make it. Sometimes.
    >


    That's right! Keep cool.

    You could make this array a typedef and then when your function
    carries a pointer to this typedef, you can dereference it
    for the size.

    #include <stdio.h>

    typedef struct _XY
    {
    int x;
    int y;
    } _XY[32];

    void size_of_xy(_XY *a)
    {
    printf("The array has size %u,"
    " Each of the %u elements has size %u\n"
    "The pointer to the array has size %u\n",
    sizeof *a,sizeof *a/sizeof(struct _XY),
    sizeof(struct _XY),sizeof a);
    }

    int main(void)
    {
    _XY _xy;
    size_of_xy(&_xy);
    return 0;
    }



    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Jul 18, 2004
    #10
  11. On Sun, 18 Jul 2004 09:27:36 -0400, Al Bowers <>
    wrote:

    >
    >
    >ak wrote:
    >>>I suggestes in my first reply, and repeated in the second, that you
    >>>should look to the FAQ, which you have obviously not done. How big a
    >>>hint do you need?
    >>>If you would *READ THE DAMN FAQ* you wouldn't keep asking these stupid
    >>>questions.

    >>
    >>
    >> Stay cool.
    >> I'll make it. Sometimes.
    >>

    >
    >That's right! Keep cool.
    >
    >You could make this array a typedef and then when your function
    >carries a pointer to this typedef, you can dereference it
    >for the size.
    >
    >#include <stdio.h>
    >
    >typedef struct _XY
    >{
    > int x;
    > int y;
    >} _XY[32];
    >
    >void size_of_xy(_XY *a)
    >{
    > printf("The array has size %u,"
    > " Each of the %u elements has size %u\n"
    > "The pointer to the array has size %u\n",
    > sizeof *a,sizeof *a/sizeof(struct _XY),
    > sizeof(struct _XY),sizeof a);
    >}


    sizeof returns a size_t which need not be an unsigned int. If you
    want to use %u to print the results, then you should cast the values.

    You have three formats in the format string but four arguments that
    follow. The first following argument (size *a) should be deleted.

    Also note that to refer to an actual structure in the array, this
    function would need to specify a[0]. This is because a is a
    pointer to the array so a[0] is the array at that address and a[0]
    is the i-th element of the array.

    >
    >int main(void)
    >{
    > _XY _xy;
    > size_of_xy(&_xy);
    > return 0;
    >}




    <<Remove the del for email>>
     
    Barry Schwarz, Jul 18, 2004
    #11
  12. "ak" <> writes:
    > > If you can't figure out that
    > > the argument given to array_size_info in do_something is a pointer and
    > > not an array, may God have mercy on your soul.
    > >

    > sure, he has.
    >
    > why I can't get size of array if I have only pointer to it?


    Have you read the FAQ?

    You have a pointer to the first element of the array, not a pointer to
    the array itself.

    Have you read the FAQ?

    --
    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.
     
    Keith Thompson, Jul 18, 2004
    #12
  13. ak

    ak Guest


    > Have you read the FAQ?

    not yet, sorry

    > You have a pointer to the first element of the array, not a pointer to
    > the array itself.


    I thought there is no difference in c between pointer to array and pointer
    to first element of array.

    Andrei
     
    ak, Jul 18, 2004
    #13
  14. ak

    Joe Wright Guest

    ak wrote:
    >>Have you read the FAQ?

    >
    > not yet, sorry
    >
    >
    >>You have a pointer to the first element of the array, not a pointer to
    >>the array itself.

    >
    >
    > I thought there is no difference in c between pointer to array and pointer
    > to first element of array.
    >
    > Andrei


    Why on earth would you think that? Consider..

    int (*ap)[10]; /* a pointer to array 10 of int */
    int *ip; /* a pointer to int */

    Do the declarations of the two pointers even look the same?

    Don't fight us Andrei, read something!
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Jul 18, 2004
    #14
  15. ak a formulé la demande :
    >> Have you read the FAQ?

    > not yet, sorry
    >
    >> You have a pointer to the first element of the array, not a pointer to
    >> the array itself.

    >
    > I thought there is no difference in c between pointer to array and pointer
    > to first element of array.
    >

    The value is the same (probably) , but the type is different.

    int main (void)
    {
    char s[123];

    /* a pointer to a char */
    char *psa = &s[0];
    char *psb = s + 0;
    char *psc = s;
    char *psd = &s; /* warning */

    /* a pointer to an array of 123 char */
    char (*px)[123] = &s;
    char (*py)[123] = s; /* warning */

    return 0;
    }
     
    Emmanuel Delahaye, Jul 18, 2004
    #15
  16. "ak" <> writes:
    > > Have you read the FAQ?

    > not yet, sorry
    >
    > > You have a pointer to the first element of the array, not a pointer to
    > > the array itself.

    >
    > I thought there is no difference in c between pointer to array and pointer
    > to first element of array.


    You wouldn't think so if you'd read the FAQ.

    The purpose of the FAQ is to answer Frequently Asked Questions, so we
    don't have to spend time answering them over and over again here. A
    lot of time and effort was dedicated to writing the FAQ list (mostly
    by Steve Summit).

    We've told you repeatedly that your questions are answered in the FAQ,
    but you keep asking them.

    If you don't want to read the FAQ, or if you want to put it off until
    later, that's fine. But unti you've read it, please stop asking
    questions that have already been answered for you. It's quite rude.

    --
    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.
     
    Keith Thompson, Jul 18, 2004
    #16
    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,338
    Ron Natalie
    Oct 14, 2004
  2. Trevor

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

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    633
    CBFalconer
    Apr 10, 2004
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  4. Tuan  Bui
    Replies:
    14
    Views:
    476
    it_says_BALLS_on_your forehead
    Jul 29, 2005
  5. Replies:
    46
    Views:
    656
    Shao Miller
    Jan 14, 2013
Loading...

Share This Page