initializing struct

C

Carramba

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?
 
I

Ian Collins

Carramba said:
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}};
 
J

Joachim Schmitz

Carramba said:
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 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
 
J

Joachim Schmitz

Ian Collins said:
Carramba said:
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
 
I

Ian Collins

Joachim said:
Ian Collins said:
Carramba said:
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.
 
C

Carramba

Joachim Schmitz skrev:
> No, you have a pointer to a struct.
>
> 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 b = {1,2,3,4};
>
> drop these 2 lines, replace by
> A *a = b;

This doesn't work : "incompatible types in assignment"
> Can be done only as part of the definition, like I did above. And not to a pointer to struct.
>
> Bye, Jojo

Thank you!
 
I

Ian Collins

Carramba said:
Joachim Schmitz skrev:

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?
 
C

Carramba

Ian Collins skrev:
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?
 
J

Joachim Schmitz

Carramba said:
Joachim Schmitz skrev:

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.

This doesn't work : "incompatible types in assignment"
A *a = &b;
to a pointer to struct.

Bye, Jojo
 
B

Ben Bacarisse

Carramba said:
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.
 
K

Keith Thompson

Carramba said:
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!). :cool:}

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.
 
B

Barry Schwarz

Ian Collins said:
Carramba said:
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
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top