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

initializing struct

P: n/a
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
Share this Question
Share on Google+
14 Replies


P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.