Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sizeof struct array

Reply
Thread Tools

sizeof struct array

 
 
ak
Guest
Posts: n/a
 
      07-17-2004
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


 
Reply With Quote
 
 
 
 
S.Tobias
Guest
Posts: n/a
 
      07-17-2004
ak <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      07-18-2004
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
 
Reply With Quote
 
ak
Guest
Posts: n/a
 
      07-18-2004
> /* 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


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      07-18-2004
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.

>

 
Reply With Quote
 
ak
Guest
Posts: n/a
 
      07-18-2004
> 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


 
Reply With Quote
 
Alex Fraser
Guest
Posts: n/a
 
      07-18-2004
"ak" <(E-Mail Removed)> wrote in message news:cddd6p$rk5$(E-Mail Removed)...
> > 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


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      07-18-2004
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.
 
Reply With Quote
 
ak
Guest
Posts: n/a
 
      07-18-2004
> 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


 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      07-18-2004


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: http://www.velocityreviews.com/forums/(E-Mail Removed) (remove the x to send email)
http://www.geocities.com/abowers822/

 
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
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments