structure initialization

A

aarklon

Hi all,

why the following structure initialization is not valid

#include<stdio.h>
struct rec
{
char name[10];
int age;
};
struct rec r;

int main(void)
{

r = {"Tom",32};
puts(r.name);

return 0;
}
 
V

Vladimir S. Oka

Hi all,

why the following structure initialization is not valid

Because the compiler does not know what type {"Tom",32} is.
#include<stdio.h>
struct rec
{
char name[10];
int age;
};
struct rec r;

int main(void)
{

r = {"Tom",32};

You could do:

r = (struct rec){"Tom",32};

Whether this is good style is another matter...
 
M

Martin Ambuhl

Hi all,

why the following structure initialization is not valid

It's not an initialization, but an assignment.

But this code, containing an initialization is OK:
#include <stdio.h>

struct rec
{
char name[10];
int age;
};

int main(void)
{
struct rec r = { "Tom", 32 };
puts(r.name);
return 0;
}


And with a C99 compiler, this code with an assignment is OK.

#include <stdio.h>

struct rec
{
char name[10];
int age;
};
struct rec r;

int main(void)
{
r = (struct rec) {"Tom", 32};
puts(r.name);

return 0;
}


[OP's code]
#include<stdio.h>
struct rec
{
char name[10];
int age;
};
struct rec r;

int main(void)
{

r = {"Tom",32};
puts(r.name);

return 0;
}
 
R

Richard G. Riley

"(e-mail address removed)"posted the following on 2006-03-09:
Hi all,

why the following structure initialization is not valid

#include<stdio.h>
struct rec
{
char name[10];
int age;
};
struct rec r;

int main(void)
{

r = {"Tom",32};
puts(r.name);

return 0;
}

Because it doesnt know that you are assigning a "struct rec" to r. It
needs to be explicitly told so that it can copy the right memory size
to your rec "r";

Change it so:

r = (struct rec){"Tom",32};

Or alternatively set the fields on their own using strcpy for name and
standard assignment for age. The direct assignment of the struct is
fast I think. If you are using gdb, step through and see how it works with

display/i $pc

and then

si
 
W

white.crow

this type of definition is static and according to Ritchie such
definitions sshould be done during declaration of the variable itself.
 
V

Vladimir S. Oka

this type of definition is static and according to Ritchie such
definitions sshould be done during declaration of the variable itself.

What type of definition? Ritchie? Guy Ritchie?

Quote what a nd who you're replying to or many people won't know what
you're talking about, and will ignore you. Read:
<http://cfaj.freeshell.org/google/>.
 
K

Keith Thompson

white.crow said:
this type of definition is static and according to Ritchie such
definitions sshould be done during declaration of the variable itself.

What type of definition?

You should follow a newsgroup for a while, or browse the archives,
before posting. If you had done so here, you could have seen several
*hundred* references to <http://cfaj.freeshell.org/google/>, which
tells you how and why to provide sufficient context when posting
through groups.google.com.

Read <http://cfaj.freeshell.org/google/>. Read it now. Tell your
friends. And ask Google to fix their broken interface, so we can stop
having to remind poeple to read <http://cfaj.freeshell.org/google/>.

That's <http://cfaj.freeshell.org/google/>.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top