473,473 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

structure initialization

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;
}

Mar 9 '06 #1
5 3998
On Thursday 09 March 2006 19:54, aa*****@gmail.com opined (in
<11*********************@v46g2000cwv.googlegroups. com>):
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...

--
BR, Vladimir

Cthulhu Cthucks!

Mar 9 '06 #2
aa*****@gmail.com wrote:
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;
}

Mar 9 '06 #3
this type of definition is static and according to Ritchie such
definitions sshould be done during declaration of the variable itself.

Mar 9 '06 #4
On Thursday 09 March 2006 22:12, white.crow opined (in
<11*********************@j52g2000cwj.googlegroups. com>):
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/>.

--
BR, Vladimir

A.A.A.A.A.: An organisation for drunks who drive.

Mar 9 '06 #5
"white.crow" <c.*****@gmail.com> writes:
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/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Read <http://cfaj.freeshell.org/google/>
Mar 9 '06 #6

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

Similar topics

2
by: Kevin P. Fleming | last post by:
I've got an enum and a structure: enum option_type { O_STRING, O_BOOL, O_NUMBER }; struct option_s { char *name;
9
by: Skybuck Flying | last post by:
Hello, What does Const mean in this c structure ? and what is the delphi equivalent ? I think const struct just means it can't be modified... is that correct ? Struct { Type1 Field1;...
2
by: kimimaro | last post by:
hi I wonder if array can be work along with structure? Below are the declaration of my structure struct employee{ char ID; char Name; char Department;
14
by: Stef | last post by:
Hello, I have a question: Is it possible to init a 2d array of structures to zero ? For example with array I do. int array = {0} but:
11
by: aaragon | last post by:
Hi everyone. I'm trying to write a class with policy based design (Alexandrescu's Modern C++ Design). I'm not a programmer but an engineer so this is kind of hard for me. Through the use of...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
4
by: koffee | last post by:
Hi people, I've got a question on global structure initialization. A global variable like int can be explicitly initialized as: int Global = 1; But how can I explicitly initialize a...
17
by: jb.simon | last post by:
Recently I was pinged in a code review about my use of the initialization method AStruct myStruct = { 0 } ; Which initializes all elements of the myStruct to 0. I was questioned on it because...
14
by: Bill Reid | last post by:
OK, let's say that have a function menu structure declaration like this (since I basically do): typedef struct function_item { char *descrip; void(*function)(void); } t_function_item; ...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.