473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

structure

23
i have different structure definitions.could any one tell me why it is not taking the folowing variations

Expand|Select|Wrap|Line Numbers
  1. ex1:
  2. #include<stdio.h>
  3.  
  4. struct{
  5. int i;              // 4 bytes
  6. char c;          // 1 byte
  7. char b;          // 1 byte
  8. };
  9.  
  10. main()
  11.  {
  12. struct a p;
  13.  
  14.  
  15.  
  16.   printf("%d",sizeof(p));
  17. }
gcc produces the given error :storage size of `p' isn't known

ex2.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. struct {
  4. int i;              // 4 bytes
  5. char c;          // 1 byte
  6. char b;          // 1 byte
  7. }a;
  8.  
  9. main()
  10.  {
  11. struct a p;
  12.  
  13.  
  14.  
  15.   printf("%d",sizeof(p));
  16. }
gcc produces "storage size of `p' isn't known"

could any one tell me why this above are wrong?
Oct 3 '06 #1
5 1543
tyreld
144 100+
You are using sizeof wrong. It expects a type to be passed to it, but you are passing an actual variable.

Expand|Select|Wrap|Line Numbers
  1. struct a p;
  2.  
  3. // Wrong
  4. sizeof(p);
  5.  
  6. // Correct
  7. sizeof(struct a);
  8.  
Oct 3 '06 #2
Banfa
9,065 Expert Mod 8TB
You are using sizeof wrong. It expects a type to be passed to it, but you are passing an actual variable.

Expand|Select|Wrap|Line Numbers
  1. struct a p;
  2.  
  3. // Wrong
  4. sizeof(p);
  5.  
  6. // Correct
  7. sizeof(struct a);
  8.  
No this is wrong sizeof can take a type or a variable and most people suggest using the variable name because then if the type changes the code stays correct with any further changes.

The problem lies in the way the structure is declared

Expand|Select|Wrap|Line Numbers
  1. struct {
  2.   int i;              // 4 bytes
  3.   char c;          // 1 byte
  4.   char b;          // 1 byte
  5. }a;
  6.  
  7. struct a p;
  8.  
This does not define a type of structure named a, it defines a variable a that is a structure you need

Expand|Select|Wrap|Line Numbers
  1. struct a {
  2.   int i;              // 4 bytes
  3.   char c;          // 1 byte
  4.   char b;          // 1 byte
  5. };
  6.  
  7. struct a p;
  8.  
This defines a type of structure named a which can then be used to define a variable p of that type.


P.S.

main()

should be

int main()

to avoid undefined behaviour
Oct 3 '06 #3
tyreld
144 100+
Banfa, you are very much correct. Thanks for catching that. Past my bedtime it would seem.
Oct 3 '06 #4
koder
23
No this is wrong sizeof can take a type or a variable and most people suggest using the variable name because then if the type changes the code stays correct with any further changes.

The problem lies in the way the structure is declared

Expand|Select|Wrap|Line Numbers
  1. struct {
  2.   int i;              // 4 bytes
  3.   char c;          // 1 byte
  4.   char b;          // 1 byte
  5. }a;
  6.  
  7. struct a p;
  8.  

This does not define a type of structure named a, it defines a variable a that is a structure you need

Expand|Select|Wrap|Line Numbers
  1. struct a {
  2.   int i;              // 4 bytes
  3.   char c;          // 1 byte
  4.   char b;          // 1 byte
  5. };
  6.  
  7. struct a p;
  8.  
This defines a type of structure named a which can then be used to define a variable p of that type.


P.S.

main()

should be

int main()

to avoid undefined behaviour
hai Banfa, really glad to see u explain the things from basics.But we have a rule that we dont need to give structure name,after the keyword struct,..in that case how i will have a structure definition and have a variable if that structure type
Oct 4 '06 #5
Banfa
9,065 Expert Mod 8TB
hai Banfa, really glad to see u explain the things from basics.But we have a rule that we dont need to give structure name,after the keyword struct,..in that case how i will have a structure definition and have a variable if that structure type
If you don't put a name after the struct keyword you have not named the structure type and will not be able to declare variables as

struct a p;

However if you don't want to name you structures you can use a typedef to define a new type for the structure

Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2.   int i;              // 4 bytes
  3.   char c;          // 1 byte
  4.   char b;          // 1 byte
  5. } a;
  6.  
a is now a new type (although very poorly named) that is the structure, you can then declare variables

Expand|Select|Wrap|Line Numbers
  1. a p;
  2.  
Oct 4 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: vikas | last post by:
I have following structure in c++. typedef struct MMF_result_struct { int action; char text; int cols,rows; int month,day,year; } MMF_result; Now this structure is shared between C++ and C#...
4
by: marco_segurini | last post by:
Hi, From my VB program I call a C++ function that gets a structure pointer like parameter. The structure has a field that contains the structure length and other fields. My problem is that...
8
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
15
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is...
3
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
14
by: Dennis | last post by:
If I have a structure like; Public Structure myStructureDef Public b() as Byte Public t as String End Structure If I pass this structure, will the values in the array b be stored on the...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
11
by: Lance | last post by:
Hi all, I've got a some structures defined as ////// <StructLayout(LayoutKind.Sequential)Public Structure GM_LayerInfo_t Public mDescription As String Public mNativeRect As GM_Rectangle_t...
4
by: eBob.com | last post by:
In my class which contains the code for my worker thread I have ... Public MustInherit Class Base_Miner #Region " Delegates for accessing main UI form " Delegate Sub DelegAddProgressBar(ByVal...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.