473,796 Members | 2,560 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
15 15169
On Sun, 18 Jul 2004 09:27:36 -0400, Al Bowers <xa******@rapid sys.com>
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][i]. This is because a is a
pointer to the array so a[0] is the array at that address and a[0][i]
is the i-th element of the array.

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


<<Remove the del for email>>
Nov 14 '05 #11
"ak" <sp**@imagero.c om> 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_Keit h) ks***@mib.org <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.
Nov 14 '05 #12
ak

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
Nov 14 '05 #13
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:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #14
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;
}
Nov 14 '05 #15
"ak" <sp**@imagero.c om> 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_Keit h) ks***@mib.org <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.
Nov 14 '05 #16

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
4706
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
2610
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
3051
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
27
5612
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
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
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
10459
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
10236
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
10017
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
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4120
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
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.