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

How to define an array of struct nested within a struct?


Is the following code legal?

typedef stuct data_type_tag {
int var1;
int var2;
int var3;
struct {
void *p;
size_t size;
unsigned int flags;
} ptrs[64];
} data_type_t;
What I am trying to do is something like this:
int c, p;
data_type_t *data;

int main(void)
{
p = 0
data = malloc(sizeof(data_type_t));
for (c = 0; c < MAXSIZE; c++)
{
data->prts[c].size = p;
data->ptrs[c].ptr = NULL;
data->ptrs[c].flags = 0;
}
}
Is that the correct way of building a structure with a format like that?
--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Mar 29 '06 #1
7 4486
Daniel Rudy opined:
typedef stuct data_type_tag {
int var1;
int var2;
int var3;
struct {
void *p;
size_t size;
unsigned int flags;
} ptrs[64];
} data_type_t;
What I am trying to do is something like this:
int c, p;
data_type_t *data;

int main(void)
{
p = 0
data = malloc(sizeof(data_type_t));
for (c = 0; c < MAXSIZE; c++)
{
data->prts[c].size = p;
data->ptrs[c].ptr = NULL;
data->ptrs[c].flags = 0;
}
}


It looks OK. You did re-type instead of copy/paste. Don't do that.

I'd also suggest you define the inner structure outside, and use that
in the declaration of the final one. Much cleaner, and very useful if
you ever need to use it in other places as well.

--
BR, Vladimir

Those aren't WINOS -- that's my JUGGLER, my AERIALIST, my SWORD
SWALLOWER, and my LATEX NOVELTY SUPPLIER!!

Mar 29 '06 #2
At about the time of 3/28/2006 11:04 PM, Vladimir S. Oka stated the
following:
Daniel Rudy opined:
typedef stuct data_type_tag {
int var1;
int var2;
int var3;
struct {
void *p;
size_t size;
unsigned int flags;
} ptrs[64];
} data_type_t;
What I am trying to do is something like this:
int c, p;
data_type_t *data;

int main(void)
{
p = 0
data = malloc(sizeof(data_type_t));
for (c = 0; c < MAXSIZE; c++)
{
data->prts[c].size = p;
data->ptrs[c].ptr = NULL;
data->ptrs[c].flags = 0;
}
}
It looks OK.


Thanks. I wasn't sure how it should look, and none of my reference
materials clearly explained this particular situation.
You did re-type instead of copy/paste. Don't do that.
The code is a simplified example to illustrate what I am trying to do.
I'd also suggest you define the inner structure outside, and use that
in the declaration of the final one. Much cleaner, and very useful if
you ever need to use it in other places as well.

Thanks for looking it over.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Mar 29 '06 #3

Daniel Rudy wrote:
At about the time of 3/28/2006 11:04 PM, Vladimir S. Oka stated the
following:
Daniel Rudy opined:

<snip>
You did re-type instead of copy/paste. Don't do that.
The code is a simplified example to illustrate what I am trying to do.


Making sure it's syntactically correct makes it easier for someone
who'd like to copy/paste and run the example. It may not have been as
important in this case, but will generally increase the number and
quality of replies to questions.

<snip>
Thanks for looking it over.


You're welcome. :-)

--
BR, Vladimir

Mar 29 '06 #4

Daniel Rudy wrote:
Is the following code legal?

typedef stuct data_type_tag {
int var1;
int var2;
int var3;
struct {
void *p;
size_t size;
unsigned int flags;
} ptrs[64];
} data_type_t;
What I am trying to do is something like this:
int c, p;
data_type_t *data;

int main(void)
{
p = 0
I'am afraid you just have missed a ";". data = malloc(sizeof(data_type_t));
for (c = 0; c < MAXSIZE; c++)
{
data->prts[c].size = p;
data->ptrs[c].ptr = NULL;
data->ptrs[c].flags = 0;
}
Well, it will be better to add a statement "return 0;". }
Is that the correct way of building a structure with a format like that?
OK, basically I agree with your method.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep


Mar 29 '06 #5

Daniel Rudy wrote:
Is the following code legal?

typedef stuct data_type_tag {
int var1;
int var2;
int var3;
struct {
void *p;
size_t size;
unsigned int flags;
} ptrs[64];
} data_type_t;

Perfectly legal, although I'd do

struct inner {
void *p;
size_t size;
unsigned int flags;
};

typedef struct data_type_tag {
int var1;
int var2;
int var3;
struct inner ptrs[64];
} data_type_t;

Keeps things a little cleaner.

What I am trying to do is something like this:
int c, p;
data_type_t *data;

int main(void)
{
p = 0
data = malloc(sizeof(data_type_t));
Personally, I'd write that as

data = malloc(sizeof *data);

That way you don't have to worry about syncing the data type with the
item being allocated.
for (c = 0; c < MAXSIZE; c++)
{
data->prts[c].size = p;
data->ptrs[c].ptr = NULL;
data->ptrs[c].flags = 0;
}
}
Is that the correct way of building a structure with a format like that?


Looks okay to me.

Mar 29 '06 #6
Daniel Rudy wrote:
Is the following code legal?

typedef stuct data_type_tag {
int var1;
int var2;
int var3;
struct {
void *p;
size_t size;
unsigned int flags;
} ptrs[64];
} data_type_t;
What I am trying to do is something like this:
int c, p;
data_type_t *data;

int main(void)
{
p = 0
data = malloc(sizeof(data_type_t));
for (c = 0; c < MAXSIZE; c++)
{
data->prts[c].size = p;
data->ptrs[c].ptr = NULL;
data->ptrs[c].flags = 0;
}
}

It is legal as long as MAXSIZE < 64.

Karthik
Is that the correct way of building a structure with a format like that?
--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep


Mar 31 '06 #7
At about the time of 3/30/2006 9:47 PM, ka*****@gmail.com stated the
following:
Daniel Rudy wrote:
Is the following code legal?

typedef stuct data_type_tag {
int var1;
int var2;
int var3;
struct {
void *p;
size_t size;
unsigned int flags;
} ptrs[64];
} data_type_t;
What I am trying to do is something like this:
int c, p;
data_type_t *data;

int main(void)
{
p = 0
data = malloc(sizeof(data_type_t));
for (c = 0; c < MAXSIZE; c++)
{
data->prts[c].size = p;
data->ptrs[c].ptr = NULL;
data->ptrs[c].flags = 0;
}
}


It is legal as long as MAXSIZE < 64.

Karthik
Is that the correct way of building a structure with a format like that?


True. Like I said earlier, I wrote this code on the fly as an example.
I was looking for the basic format of how to get it done.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Mar 31 '06 #8

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

Similar topics

1
by: mrhicks | last post by:
Hello all, I need some advice/help on a particular problem I am having. I have a basic struct called "indv_rpt_rply" that holds information for a particular device in our system which I will...
12
by: shailashri_sk | last post by:
Hi, int *p; p++; here p now increments itself with the size of integer. similarly, I wanted to know, how to declare an pointer to an array ( say array of integers) where in it we do a p++ it...
0
by: Peter Demeyer | last post by:
I have this problem: 'pno' is a pointer to a struct of type PRINTER_NOTIFY_OPTIONS. This struct is holding another struct called 'not' (of type PRINTER_NOTIFY_OPTIONS_TYPE) which contains a couple...
2
by: symbol | last post by:
I am having this problem in a managed c++ DLL which mixes managed and unmanaged C/C++ code. I tried to assign value to a struct array nested in another struct. but I can only write to the first...
1
by: symbol | last post by:
I am having this problem in a managed c++ DLL which mixes managed and unmanaged C/C++ code. I tried to assign value to a struct array nested in another struct. but I can only write to the first...
2
by: James Brown | last post by:
All, Could anyone explain why the following is an error: void foo( struct FOO { int x; } f1 ) { } int main()
4
by: _mario.lat | last post by:
for struct: struct in6_addr { uint8_t s6_addr; }; is provided a costant: #define IN6ADDR_LOOPBACK_INIT {{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}}} what does means {{{, and }}}?
2
by: Opteron64 | last post by:
Hi, I'm trying to create and initialise a dynamic array within a nested structure. The structure is defined as followed: (C++ code) typedef unsigned char uchar; typedef unsigned int uint; ...
5
by: maker.rain1 | last post by:
Hello All, I have come across a problem as explained below in a sample. Please help me if anyone has any ideas to solve this. I have a #define as defined below. #define MAX 200 int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.