473,785 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof struct array

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

size_of_xy(stru ct 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
Nov 14 '05 #1
15 15168
ak <sp**@imagero.c om> wrote:
struct xy{
int x;
int y;
}
_xy[32]; size_of_xy(stru ct 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_arra y(struct xy_array *a) {
printf("size: %u\n", (unsigned)(size of(struct xy) * a->xy_arr_len)) ;
}

--
Stan Tobias
sed '/[A-Z]//g' to email
Nov 14 '05 #2
ak wrote:
struct xy{
int x;
int y;
}
_xy[32];

size_of_xy(stru ct 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.c om. 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
Nov 14 '05 #3
ak
> /* mha: you could read the FAQ before posting. Or if that's beyond
you, check the archives at groups.google.c om. 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(st ruct xy *_xy) {
array_size_info (_xy);
}

int main( void ) {
array_size_info ( first_xy );
array_size_info ( second_xy );
do_something(fi rst_xy);
do_something(se cond_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
Nov 14 '05 #4
ak wrote:
/* mha: you could read the FAQ before posting. Or if that's beyond
you, check the archives at groups.google.c om. 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(st ruct xy *_xy) {
array_size_info (_xy);
}

int main( void ) {
array_size_info ( first_xy );
array_size_info ( second_xy );
do_something(fi rst_xy);
do_something(se cond_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.

Nov 14 '05 #5
ak
> 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
Nov 14 '05 #6
"ak" <sp**@imagero.c om> wrote in message news:cd******** **@online.de...
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
Nov 14 '05 #7
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.
Nov 14 '05 #8
ak
> 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
Nov 14 '05 #9


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: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7203
by: Zero | last post by:
If we have a structure like: struct something{ int *a; int b; }; We allocate mempry for a using malloc or calloc. The question is when we want to know the size of the structure, sizeof(struct something), it will not give us the correct number (i.e. the size of the structure
10
2434
by: Sean | last post by:
I have a struct that I wrote to test a protocol. The idea I had was to just declare the elements of the struct in the order in which they are sent and received as defined by the protocol. However, writing this struct to a file produces unexpected results. Here is a test struct I wrote: struct Tester { unsigned short first; unsigned int second;
12
2118
by: news.fe.internet.bosch.com | last post by:
Hi All , I u find out size of struct , does it considers paddding chars into consideration struct A { char c; int i; };
6
10820
by: SB | last post by:
I feel dumb to ask because I bet this is a simple question... Looking at the code below, can someone please explain why I get two different values in my Marshal.SizeOf calls (see the commented lines)? TIA! sb
74
4698
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique seems to be setting a NULL pointer to the end of the list, and here we know that the allocated memory has been exhausted. All good. When this is a pointer to another type, say int, I could have a variable that records how much memory is being...
32
2593
by: Abhishek Srivastava | last post by:
Hi, Somebody recently asked me to implement the sizeof operator, i.e. to write a function that accepts a parameter of any type, and without using the sizeof operator, should be able to return the size occupied by that datatype in memory in bytes. Thanks :) Abhishek Srivastava
38
2609
by: James Brown | last post by:
All, I have a quick question regarding the size of pointer-types: I believe that the sizeof(char *) may not necessarily be the same as sizeof(int *) ? But how about multiple levels of pointers to the same type? Would sizeof(char **) be the same as sizeof(char *)? And if it is, would the internal representation be the same in both cases? background on this: I'm writing a simple IDL compiler that produces 'C'
72
3047
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
27
5609
by: CodeMonk3y | last post by:
gotta question on sizeof keyword does the sizeof keyword calcuates the size at compile time or run time ?? -- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
0
9483
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.