Initialization of Structs - Typedefs

M

Mahendra

I have the following structs -

15 typedef struct pSimple {
16 char *key;
17 char *val;
18 } pSimple, *pSimple_ptr;
19
20 typedef struct pComplex {
21 char *name;
22 int value;
23 } pComplex, *pComplex_ptr;
24
25 typedef struct pNode {
26 char *nodeId;
27 struct pSimple *semStats;
28 struct pComplex *sysStats;
29 } pNode, *pNode_ptr;

Now somewhere in my main() i do -

pNode data;

and then call generate_data(&data); where i want to initialize the
"data". Now the question i have is -

1. When i do- pNode data, does it allocate struct pNode ? If yes, it is
in stack ? If not what exactly does it does ? Can i just use &data to
initialize the struct pNode or do i have to allocate heap memory for it ?

Thanks
Mahendra
 
B

Barry Schwarz

Mahendra said:
I have the following structs -
[...]
25 typedef struct pNode {
26 char *nodeId;
27 struct pSimple *semStats;
28 struct pComplex *sysStats;
29 } pNode, *pNode_ptr;

Now somewhere in my main() i do -

pNode data;

and then call generate_data(&data); where i want to initialize the
"data". Now the question i have is -

1. When i do- pNode data, does it allocate struct pNode ? If yes, it is
in stack ? If not what exactly does it does ? Can i just use &data to
initialize the struct pNode or do i have to allocate heap memory for it ?

Yes, probably, doesn't matter, yes, no.

Yes, the declaration allocates storage for the thing declared,

definition and defined instead of declaration and declared
 
E

Eric Sosman

Barry said:
Mahendra said:
I have the following structs -
[...]
25 typedef struct pNode {
26 char *nodeId;
27 struct pSimple *semStats;
28 struct pComplex *sysStats;
29 } pNode, *pNode_ptr;

Now somewhere in my main() i do -

pNode data;

and then call generate_data(&data); where i want to initialize the
"data". Now the question i have is -

1. When i do- pNode data, does it allocate struct pNode ? If yes, it is
in stack ? If not what exactly does it does ? Can i just use &data to
initialize the struct pNode or do i have to allocate heap memory for it ?
Yes, probably, doesn't matter, yes, no.

Yes, the declaration allocates storage for the thing declared,

definition and defined instead of declaration and declared

Given the absence of the `extern' keyword, I stand by
my nomenclature.
 
B

Barry Schwarz

Barry said:
Mahendra wrote:
I have the following structs -
[...]
25 typedef struct pNode {
26 char *nodeId;
27 struct pSimple *semStats;
28 struct pComplex *sysStats;
29 } pNode, *pNode_ptr;

Now somewhere in my main() i do -

pNode data;

and then call generate_data(&data); where i want to initialize the
"data". Now the question i have is -

1. When i do- pNode data, does it allocate struct pNode ? If yes, it is
in stack ? If not what exactly does it does ? Can i just use &data to
initialize the struct pNode or do i have to allocate heap memory for it ?
Yes, probably, doesn't matter, yes, no.

Yes, the declaration allocates storage for the thing declared,

definition and defined instead of declaration and declared

Given the absence of the `extern' keyword, I stand by
my nomenclature.

But if extern had been part of the code, it would definitely be a
declaration and not a definition. And of course it would not have
allocated storage. That it did allocate storage is one of the clear
distinctions between declarations and definitions.
 
G

George

Barry said:
Mahendra wrote:
I have the following structs -
[...]
25 typedef struct pNode {
26 char *nodeId;
27 struct pSimple *semStats;
28 struct pComplex *sysStats;
29 } pNode, *pNode_ptr;

Now somewhere in my main() i do -

pNode data;

and then call generate_data(&data); where i want to initialize the
"data". Now the question i have is -

1. When i do- pNode data, does it allocate struct pNode ? If yes, it is
in stack ? If not what exactly does it does ? Can i just use &data to
initialize the struct pNode or do i have to allocate heap memory for it ?
Yes, probably, doesn't matter, yes, no.

Yes, the declaration allocates storage for the thing declared,

definition and defined instead of declaration and declared

Given the absence of the `extern' keyword, I stand by
my nomenclature.

But if extern had been part of the code, it would definitely be a
declaration and not a definition. And of course it would not have
allocated storage. That it did allocate storage is one of the clear
distinctions between declarations and definitions.

Of what order is the node created?
--
George

When you turn your heart and your life over to Christ, when you accept
Christ as the savior, it changes your heart.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
B

Barry Schwarz

Barry Schwarz wrote:
Mahendra wrote:
I have the following structs -
[...]
25 typedef struct pNode {
26 char *nodeId;
27 struct pSimple *semStats;
28 struct pComplex *sysStats;
29 } pNode, *pNode_ptr;

Now somewhere in my main() i do -

pNode data;

and then call generate_data(&data); where i want to initialize the
"data". Now the question i have is -

1. When i do- pNode data, does it allocate struct pNode ? If yes, it is
in stack ? If not what exactly does it does ? Can i just use &data to
initialize the struct pNode or do i have to allocate heap memory for it ?
Yes, probably, doesn't matter, yes, no.

Yes, the declaration allocates storage for the thing declared,

definition and defined instead of declaration and declared

Given the absence of the `extern' keyword, I stand by
my nomenclature.

But if extern had been part of the code, it would definitely be a
declaration and not a definition. And of course it would not have
allocated storage. That it did allocate storage is one of the clear
distinctions between declarations and definitions.

Of what order is the node created?

What do you mean by order? If you mean chronological order, then the
object data (which is defined in main according to the OP and has
automatic storage duration) is created (and an indeterminate value
stored in it) each time its definition is encountered during the
course of execution as described in 6.8-3.

What do you mean by node? The object data has type struct pNode. The
word node has no special meaning in the standard. Frequently it is
used here to describe an element of a linked list but since this
structure does not contain a pointer to another struct pNode it cannot
be an element of a linked list.
 
G

George

On Mon, 03 Nov 2008 20:41:58 -0500, Eric Sosman

Barry Schwarz wrote:
Mahendra wrote:
I have the following structs -
[...]
25 typedef struct pNode {
26 char *nodeId;
27 struct pSimple *semStats;
28 struct pComplex *sysStats;
29 } pNode, *pNode_ptr;

Now somewhere in my main() i do -

pNode data;

and then call generate_data(&data); where i want to initialize the
"data". Now the question i have is -

1. When i do- pNode data, does it allocate struct pNode ? If yes, it is
in stack ? If not what exactly does it does ? Can i just use &data to
initialize the struct pNode or do i have to allocate heap memory for it ?
Yes, probably, doesn't matter, yes, no.

Yes, the declaration allocates storage for the thing declared,

definition and defined instead of declaration and declared

Given the absence of the `extern' keyword, I stand by
my nomenclature.

But if extern had been part of the code, it would definitely be a
declaration and not a definition. And of course it would not have
allocated storage. That it did allocate storage is one of the clear
distinctions between declarations and definitions.

Of what order is the node created?

What do you mean by order? If you mean chronological order, then the
object data (which is defined in main according to the OP and has
automatic storage duration) is created (and an indeterminate value
stored in it) each time its definition is encountered during the
course of execution as described in 6.8-3.

I've been talking about these data structures with a friend who has a more
hands-on relationship to it. In graph theory, the order of the node is the
number of edges it has. So it is with a linked list that you see a line
going from one vertex (node) to another. I was thinking, maybe in C, that
any or all of these pointers, that I don't really get, could be interpreted
as lines, aka edges.

What do you mean by node? The object data has type struct pNode. The
word node has no special meaning in the standard. Frequently it is
used here to describe an element of a linked list but since this
structure does not contain a pointer to another struct pNode it cannot
be an element of a linked list.

Isn't there a picture that has vertices and edges that this suggests? I
wouldn't expect the standard to address combinatorics.


--
George

I am mindful not only of preserving executive powers for myself, but for
predecessors as well.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 

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

Forum statistics

Threads
474,260
Messages
2,571,039
Members
48,768
Latest member
first4landlord

Latest Threads

Top