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

D

Daniel Rudy

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
 
V

Vladimir S. Oka

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.
 
D

Daniel Rudy

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
 
V

Vladimir S. Oka

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

said:
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.

said:
Thanks for looking it over.

You're welcome. :)
 
C

Captain Winston

Daniel said:
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.
 
J

John Bode

Daniel said:
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.
 
K

kar1107

Daniel said:
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
 
D

Daniel Rudy

At about the time of 3/30/2006 9:47 PM, (e-mail address removed) stated the
following:
Daniel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top