473,403 Members | 2,284 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,403 software developers and data experts.

C structures

6
Hi,

I have three questions about C structures.

First,
How do I calculate the total number of members in the structure?
for example,
struct A{
int a;
int b;
}
Is it equal to sizeof(A)/sizeof(int)? But this cannot be generalized if a struct has members of different types.
How do I do this?

Second,
Is it possible to define new members to struct later after the definition, like maybe in main()?

Third,
I have tried to reintialize a struct variable using
struct A test={0,2};

Later, it is changed to test={3,5};
This gives me error saying parse error. It doesnt give any error if I explicitly intialize the individual members like test.a=3 test.b=5. Why it dint work before?

Thanks!
Oct 6 '06 #1
4 2205
Banfa
9,065 Expert Mod 8TB
First,
How do I calculate the total number of members in the structure?

You can't, not only can you not do this but I can't think of any reason why you'd want to.

Second,
Is it possible to define new members to struct later after the definition, like maybe in main()?

No, but if you where to put pointers in you structures you could then allocate a variable size array depending on what was required by the program.

Third,
I have tried to reintialize a struct variable using
struct A test={0,2};

Later, it is changed to test={3,5};
This gives me error saying parse error. It doesnt give any error if I explicitly intialize the individual members like test.a=3 test.b=5. Why it dint work before?

You can only initialise a structure to an agregate ( {3,5} ) you can not use an agregate in an assignment.
Oct 7 '06 #2
MyCGal
6
First,
How do I calculate the total number of members in the structure?

You can't, not only can you not do this but I can't think of any reason why you'd want to.

Second,
Is it possible to define new members to struct later after the definition, like maybe in main()?

No, but if you where to put pointers in you structures you could then allocate a variable size array depending on what was required by the program.

Third,
I have tried to reintialize a struct variable using
struct A test={0,2};

Later, it is changed to test={3,5};
This gives me error saying parse error. It doesnt give any error if I explicitly intialize the individual members like test.a=3 test.b=5. Why it dint work before?

You can only initialise a structure to an agregate ( {3,5} ) you can not use an agregate in an assignment.
Hi,

Thanks for all your answers.

I have more Qs. Suppose I have a struct with 8 1-bit fields. I would like to display only certain number of bits. I have a display(struct) that displays all the 8 1-bit fields. Is it possible to display only certain number of bits of this struct using display(struct a, int number_of_bits_to_show)?
I was thinking of converting the bits into a equivalent deceimal value and then using masks and shifts to display the reqd number of bits.

I would appreciate your comments on this.

Thanks!
Oct 8 '06 #3
D_C
293 100+
Yeah, but say I want to show 5 bits. Which 5 bits do I show? 0-4, 1-5, 2-6, or 3-7?

Expand|Select|Wrap|Line Numbers
  1. void show_bits(const char bits, const int start, const int num_bits)
  2. {
  3.   for(int i = start; i < (start+num_bits); i++)
  4.     cout << ((bits >> i) && 1);
  5. // equivalently cout << ((bits && (1<<i))>>i);
  6.  
  7.   cout << endl;
  8. }
Oct 8 '06 #4
MyCGal
6
Hi,

Thanks for the code snippet. I assume that you have stored the 8 1-bit fields of the structure into char array "bits". And yes, I want to display any number of bits starting from anywhere in the bit string.
Oct 8 '06 #5

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

Similar topics

1
by: kazack | last post by:
Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on...
33
by: Peter Seaman | last post by:
I understand that structures are value types and arrays and classes are reference types. But what about arrays as members of structures i.e. as in C struct x { int n; int a; }
6
by: Ken Allen | last post by:
OK, I admit that I have been programming since before C++ was invented, and I have developed more than my share of assembly language systems, and even contributed to operating system and compiler...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
14
by: pmclinn | last post by:
I've noticed that many programmers use classes to store data about such things like: Class Customers .....Phone ....ID ....Address End Class....
2
by: thomasfarrow | last post by:
At work, our development team has a development standards document that insists Structures should never be used. I'm looking to change this standard but need a suitable argument in order to make...
11
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure...
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
4
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
8
by: Bob Altman | last post by:
Hi all, I have a structure that includes a constructor. I want to add a bunch of these structures to an STL map (whose index is an int). If I define the map like this: map<int,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.