473,394 Members | 1,794 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,394 software developers and data experts.

Struct inside a struct

hi,
i am making a struct within a struct like this..

struct upper
{
struct lower{
BYTE* row;
UINT width_row;
}
}

upper* table= new upper[100];
upper[x].lower* plates = new upper[x].lower[100];

how can i access each element in this way..
table[0].plates [0].width_row = 10; // for example
table[0].plates [1].width_row = 12;
table[n].plates [m].width_row = something;
.... so on

so that later i can do:

table[n].plates[m].row = new BYTE[table[n].plates [m].width_row];

Thx

Dec 18 '05 #1
3 2123
monkeydragon wrote:
hi,
i am making a struct within a struct like this..

struct upper
{
struct lower{
BYTE* row;
UINT width_row;
}
;
}
;

upper* table= new upper[100];
upper[x].lower* plates = new upper[x].lower[100];
upper::lower* plates = new upper::lower[100];
how can i access each element in this way..
table[0].plates [0].width_row = 10; // for example
table[0].plates [1].width_row = 12;
table[n].plates [m].width_row = something;
... so on
In this case, your upper class needs to contain an ary of lower, or - if you
need a dynamic size, a pointer to lower, like:

struct upper
{
struct lower
{
BYTE* row;
UINT width_row;
};

lower* plates;
};

Then you can do something like:

int main()
{
upper* table = new upper[100];
table[0].plates = new upper::lower[100];

table[0].plates[0].width_row = 10;

delete[] table[0].plates;
delete[] table;
}
However, I suggest using std::vector, which is easier to use than a dynamic
array, but gives the same functionality:

#include <vector>

struct upper
{
struct lower
{
BYTE* row;
UINT width_row;
};

std::vector<lower> plates;
};

int main()
{
std::vector<upper> table;
table.resize(100);
table[0].plates.resize(100);
table[0].plates[0].width_row = 10;
}

Note that there is no need for deleting the memory. That is done
automatically.
so that later i can do:

table[n].plates[m].row = new BYTE[table[n].plates [m].width_row];


Again, use a vector for that instead of raw arrays. It will also keep track
of its size.
Dec 18 '05 #2
"upper" get its value change every loop..
i am wondering if when you we use table.resize
say from table.resize(10) then table.resize(20);
what will happen to data from 0-9? will it be still there?

i.e.
count = 1
do{
table.plates.resize(count)
// some routine...
++count;
}while(SOME_CONDITION);

krby_xtrm

Dec 20 '05 #3
monkeydragon wrote:
"upper" get its value change every loop..
i am wondering if when you we use table.resize
say from table.resize(10) then table.resize(20);
what will happen to data from 0-9? will it be still there?
Yes. However, any pointers or iterators to the data may be invalidated.
i.e.
count = 1
do{
table.plates.resize(count)
// some routine...
++count;
}while(SOME_CONDITION);


If you want to add a value at every iteration, I'd recommend to use
push_back() instead, like:

do{
// get new value
table.plates.push_back(new_value);
}while(SOME_CONDITION);

push_back will automatically resize the vector.
Dec 20 '05 #4

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

Similar topics

5
by: uny ternally | last post by:
I was experimenting in Visual C++ and ran into the following problem. I have the struct listed below. I also have a function that passes a variable of the struct type by reference and set the...
1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
20
by: Elliot Marks | last post by:
If a struct or its members are passed to a function, must it be declared globally? #include <stdio.h> struct mystruct{ int a; int b; }; int structfunc(struct mystruct foo);
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
1
by: Jón Sveinsson | last post by:
Hello everone I have code from c which I need to convert to c#, the c code is following, as you can see there is a struct inside struct typedef struct { some variables....
6
by: Michael C | last post by:
Is it possible to use an ArrayList inside a struct? I keep running into a null reference exception when I try to Add to the ArrayList in the struct, and it won't let me initialize the ArrayList in...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
6
by: CptDondo | last post by:
I'm trying to figure out some code that uses structures, structures, and more structures... It's a bit of rat's nest, and I'm having some trouble sorting it all out. The authors use a lot of...
12
by: djhong | last post by:
Following is a snippet of a header file. Here, #define are inside struct evConn{} Any advantage of putting them inside struct block? Looks like there is no diff in scoping of each...
3
by: dreiko466 | last post by:
(sorry about my english...) I am a newbie in C (3 month expierience) I have wrote a simple test programm in VS2005, what i do wrong?Please... In this programm i create a double linked list.Then ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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,...
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...

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.