473,410 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

initializing struct

Hi!
I have a big struct and I want to initialize it at once, but I get parse
error before "{" compiler error. can't it by done?

#include <stdio.h>
#include <stdlib.h>

typedef struct{
int b;
int c;
int d;
int e;
}A;

A *a;

int main(void) {

a = malloc(4 * sizeof (*a));
a = {1,1,4,5}; //there is error from my compiler
printf("%d ; %d\n", a->b+a->d, a->c*a->e);

free(a);
return 0;
}

is this is bad way to init struct how one should do?
Jun 11 '07 #1
14 4257
Carramba wrote:
Hi!
I have a big struct and I want to initialize it at once, but I get parse
error before "{" compiler error. can't it by done?

#include <stdio.h>
#include <stdlib.h>

typedef struct{
int b;
int c;
int d;
int e;
}A;

A *a;

int main(void) {

a = malloc(4 * sizeof (*a));
a = {1,1,4,5}; //there is error from my compiler
You can't assign a static initialiser to a pointer. You could write

A a[4] = {{1},{1},{4},{5}};

--
Ian Collins.
Jun 11 '07 #2
"Carramba" <ca******@example.comschrieb im Newsbeitrag
news:f4**********@aioe.org...
Hi!
I have a big struct and I want to initialize it at once, but I get parse
No, you have a pointer to a struct.
error before "{" compiler error. can't it by done?

#include <stdio.h>
#include <stdlib.h>

typedef struct{
int b;
int c;
int d;
int e;
}A;
That's not particulary large...
>
A *a;
A b = {1,2,3,4};
int main(void) {

a = malloc(4 * sizeof (*a));
a = {1,1,4,5}; //there is error from my compiler
drop these 2 lines, replace by
A *a = b;
printf("%d ; %d\n", a->b+a->d, a->c*a->e);

free(a);
return 0;
}

is this is bad way to init struct how one should do?
Can be done only as part of the definition, like I did above. And not to a
pointer to struct.

Bye, Jojo
Jun 11 '07 #3
"Ian Collins" <ia******@hotmail.comschrieb im Newsbeitrag
news:5d**************@mid.individual.net...
Carramba wrote:
>Hi!
I have a big struct and I want to initialize it at once, but I get parse
error before "{" compiler error. can't it by done?

#include <stdio.h>
#include <stdlib.h>

typedef struct{
int b;
int c;
int d;
int e;
}A;

A *a;

int main(void) {

a = malloc(4 * sizeof (*a));
a = {1,1,4,5}; //there is error from my compiler

You can't assign a static initialiser to a pointer. You could write

A a[4] = {{1},{1},{4},{5}};
Which would set a[0].b to 1, a[1].b to 1, a[2].b to 4 and a[3].b to 5,
leaving all the c, d and e members uninitialized resp. set to 0.

A a = {1, 1, 4, 5};
Would do...
Bye, Jojo
Jun 11 '07 #4
Joachim Schmitz wrote:
"Ian Collins" <ia******@hotmail.comschrieb im Newsbeitrag
news:5d**************@mid.individual.net...
>Carramba wrote:
>>Hi!
I have a big struct and I want to initialize it at once, but I get parse
error before "{" compiler error. can't it by done?

#include <stdio.h>
#include <stdlib.h>

typedef struct{
int b;
int c;
int d;
int e;
}A;

A *a;

int main(void) {

a = malloc(4 * sizeof (*a));
a = {1,1,4,5}; //there is error from my compiler
You can't assign a static initialiser to a pointer. You could write

A a[4] = {{1},{1},{4},{5}};
Which would set a[0].b to 1, a[1].b to 1, a[2].b to 4 and a[3].b to 5,
leaving all the c, d and e members uninitialized resp. set to 0.
Correct, that's what I thought the OP wanted.

--
Ian Collins.
Jun 11 '07 #5

Joachim Schmitz skrev:
"Carramba" <ca******@example.comschrieb im Newsbeitrag
news:f4**********@aioe.org...
>Hi!
I have a big struct and I want to initialize it at once, but I get parse
No, you have a pointer to a struct.
>error before "{" compiler error. can't it by done?

#include <stdio.h>
#include <stdlib.h>

typedef struct{
int b;
int c;
int d;
int e;
}A;
That's not particulary large...
I only wrote compilable small program to show my problem...
I don't think there would by a point to post huge struct then this one
serves purpose.

>A *a;
A b = {1,2,3,4};
>int main(void) {

a = malloc(4 * sizeof (*a));
a = {1,1,4,5}; //there is error from my compiler
drop these 2 lines, replace by
A *a = b;
This doesn't work : "incompatible types in assignment"
>printf("%d ; %d\n", a->b+a->d, a->c*a->e);

free(a);
return 0;
}

is this is bad way to init struct how one should do?
Can be done only as part of the definition, like I did above. And not
to a pointer to struct.
>
Bye, Jojo
Thank you!

Jun 11 '07 #6
Carramba wrote:
>
Joachim Schmitz skrev:
>drop these 2 lines, replace by
A *a = b;

This doesn't work : "incompatible types in assignment"
Not surprising, he probably intended to write A *a = *b;

What exactly to do want to do, create a number of structures and
initialise them all to the same value, or different values?

--
Ian Collins.
Jun 11 '07 #7
Ian Collins skrev:
Carramba wrote:
>Joachim Schmitz skrev:
>>drop these 2 lines, replace by
A *a = b;
This doesn't work : "incompatible types in assignment"
Not surprising, he probably intended to write A *a = *b;
you mean A *a= &b; ?
What exactly to do want to do, create a number of structures and
initialise them all to the same value, or different values?
Jun 11 '07 #8
Carramba wrote:
Ian Collins skrev:
>Carramba wrote:
>>Joachim Schmitz skrev:
drop these 2 lines, replace by
A *a = b;
This doesn't work : "incompatible types in assignment"
Not surprising, he probably intended to write A *a = *b;
you mean A *a= &b; ?
>What exactly to do want to do, create a number of structures and
initialise them all to the same value, or different values?
oops...

--
Ian Collins.
Jun 11 '07 #9
"Carramba" <ca******@example.comschrieb im Newsbeitrag
news:f4**********@aioe.org...
Ian Collins skrev:
>Carramba wrote:
>>Joachim Schmitz skrev:
drop these 2 lines, replace by
A *a = b;
This doesn't work : "incompatible types in assignment"
Not surprising, he probably intended to write A *a = *b;
you mean A *a= &b; ?
Yes, sorry, that's what I meant.
Jun 11 '07 #10
"Carramba" <ca******@example.comschrieb im Newsbeitrag
news:f4**********@aioe.org...
>
Joachim Schmitz skrev:
"Carramba" <ca******@example.comschrieb im Newsbeitrag
news:f4**********@aioe.org...
Hi!
I have a big struct and I want to initialize it at once, but I get
parse
No, you have a pointer to a struct.
error before "{" compiler error. can't it by done?

#include <stdio.h>
#include <stdlib.h>

typedef struct{
int b;
int c;
int d;
int e;
}A;
That's not particulary large...

I only wrote compilable small program to show my problem...
I don't think there would by a point to post huge struct then this one
serves purpose.
OK, I sort of guessed that... but couldn't resist that remark.

A *a;
A b = {1,2,3,4};
int main(void) {

a = malloc(4 * sizeof (*a));
a = {1,1,4,5}; //there is error from my compiler
drop these 2 lines, replace by
A *a = b;

This doesn't work : "incompatible types in assignment"
A *a = &b;
printf("%d ; %d\n", a->b+a->d, a->c*a->e);

free(a);
return 0;
}

is this is bad way to init struct how one should do?
Can be done only as part of the definition, like I did above. And not
to a pointer to struct.
Bye, Jojo
Jun 11 '07 #11
Carramba <ca******@example.comwrites:
Hi!
I have a big struct and I want to initialize it at once, but I get
parse error before "{" compiler error. can't it by done?

#include <stdio.h>
#include <stdlib.h>

typedef struct{
int b;
int c;
int d;
int e;
}A;

A *a;

int main(void) {

a = malloc(4 * sizeof (*a));
a = {1,1,4,5}; //there is error from my compiler
printf("%d ; %d\n", a->b+a->d, a->c*a->e);

free(a);
return 0;
}

is this is bad way to init struct how one should do?
You do know you are allocating space for an array of 4 structs? If that
is indeed what you want you can do it like this:

struct A local_copy[] = {{1}, {2}, {3}, {4}};
a = malloc(4 * sizeof *a);
if (a)
memcpy(a, local_copy, sizeof local_copy);

In C99 you can use a compound literal:

a = malloc(4 * sizeof *a);
if (a)
memcpy(a, (struct A[]){{1}, {2}, {3}, {4}}, 4 * sizeof *a);

This has portability issues (limited C99 support) and is messier to
write. Note also that I have imitated other posters and explicitly
set only the "b" structure member in each array element.

--
Ben.
Jun 11 '07 #12
Carramba <ca******@example.comwrites:
I have a big struct and I want to initialize it at once, but I get
parse error before "{" compiler error. can't it by done?

#include <stdio.h>
#include <stdlib.h>

typedef struct{
int b;
int c;
int d;
int e;
}A;

A *a;

int main(void) {

a = malloc(4 * sizeof (*a));
a = {1,1,4,5}; //there is error from my compiler
printf("%d ; %d\n", a->b+a->d, a->c*a->e);

free(a);
return 0;
}

is this is bad way to init struct how one should do?
Well, since it doesn't compile, yeah, it's a bad way (but it's
quick!). 8-)}

You allocate space for 4 structures, but you ignore all but the first.
I assume your real program doesn't do that. Also, you should always
check the result of malloc, even if you just abort the program if it
fails.

The problem with your attempted assignment is that {1,1,4,5} isn't an
expression; it can be used in an initializer, but not on the right
hand side of an assignment. And it's an initializer for a structure,
not for a pointer.

So declare a const object that you can use in assignment:

const A A_init = { 1, 1, 4, 5 };

and assign its value to a[0], or *a, or whatever.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 11 '07 #13
Carramba wrote:
>
I have a big struct and I want to initialize it at once, but I
get parse error before "{" compiler error. can't it by done?

#include <stdio.h>
#include <stdlib.h>

typedef struct{
int b;
int c;
int d;
int e;
} A;

A *a;

int main(void) {
a = malloc(4 * sizeof (*a));
a = {1,1,4,5}; //there is error from my compiler
printf("%d ; %d\n", a->b+a->d, a->c*a->e);
free(a);
return 0;
}

is this is bad way to init struct how one should do?
You can only use such initialization during a declaration, and
never for malloced space. Use:

a->b = a->c = 1; a->d = 4; a->e = 5;

Alternatively, you can assign from an existing structure:

A b = {1, 1, 4, 5}, *a;
....
if (!(a = malloc(sizeof *a))) /* memory not available */;
else {
*a = b;
...

ALWAYS check the results from malloc.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 11 '07 #14
On Mon, 11 Jun 2007 11:57:20 +0200, "Joachim Schmitz"
<no*********@schmitz-digital.dewrote:
>"Ian Collins" <ia******@hotmail.comschrieb im Newsbeitrag
news:5d**************@mid.individual.net...
>Carramba wrote:
>>Hi!
I have a big struct and I want to initialize it at once, but I get parse
error before "{" compiler error. can't it by done?

#include <stdio.h>
#include <stdlib.h>

typedef struct{
int b;
int c;
int d;
int e;
}A;

A *a;

int main(void) {

a = malloc(4 * sizeof (*a));
a = {1,1,4,5}; //there is error from my compiler

You can't assign a static initialiser to a pointer. You could write

A a[4] = {{1},{1},{4},{5}};
Which would set a[0].b to 1, a[1].b to 1, a[2].b to 4 and a[3].b to 5,
leaving all the c, d and e members uninitialized resp. set to 0.
An uninitialized variable has an indeterminate value.

In cases where an aggregate is partially explicitly initialized (such
as this), the remaining variables that are not explicitly initialized
are implicitly initialized to the appropriate form of 0.
Remove del for email
Jun 12 '07 #15

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

Similar topics

17
by: Nollie | last post by:
Say you have a struct: struct MYSTRUCT { int x; int y; int w; int h; };
3
by: Joe | last post by:
Hi, I have been struggling with this issue for a couple of days and would like to know if some can give me a pointer. I want to initialize a struct with default values and depending on the...
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
4
by: ccdrbrg | last post by:
I am trying to initialize an arrary of pointers to structs with constants. Sample code: struct mystruct { char *text; int number; };
7
by: Paminu | last post by:
In the following code I am trying to initialize a pointer that is located in a struct. #include <stdlib.h> #include <stdio.h> #define KIDS 4 typedef struct test { void *content;
5
by: ScottM | last post by:
I expect this has been covered somewhere, but I can't find it. Given: class A { public: A() {...does stuff...} }; struct S { A a; };
12
by: Mik0b0 | last post by:
Hallo. Let's say, there is a structure struct struct10{ int a1; int a2; int a3; int a4; }count={ {10,20,30,40}, {50,60,70,80}
4
by: benn686 | last post by:
I have a structure that contains a union that Id like to initialize at compile time... something like: //global declare and initialize fullStructType var1 = { unionMember.union1.field1...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
27
by: Nate Eldredge | last post by:
Consider the following pseudo-code: #include <opaque.h> struct foo { int a; opaque_t op; int b; };
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.