By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,484 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,484 IT Pros & Developers. It's quick & easy.

Initialization of Structs - Typedefs

P: n/a
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
Nov 3 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Barry Schwarz wrote:
On Mon, 03 Nov 2008 11:54:16 -0500, Eric Sosman <Er*********@sun.com>
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.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Nov 4 '08 #2

P: n/a
On Mon, 03 Nov 2008 20:41:58 -0500, Eric Sosman
<es*****@ieee-dot-org.invalidwrote:
>Barry Schwarz wrote:
>On Mon, 03 Nov 2008 11:54:16 -0500, Eric Sosman <Er*********@sun.com>
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.

--
Remove del for email
Nov 4 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.