473,672 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4278
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******@examp le.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******@hotma il.comschrieb im Newsbeitrag
news:5d******** ******@mid.indi vidual.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******@hotma il.comschrieb im Newsbeitrag
news:5d******** ******@mid.indi vidual.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******@examp le.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 : "incompatib le 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 : "incompatib le 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 : "incompatib le 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 : "incompatib le 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******@examp le.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 : "incompatib le 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

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

Similar topics

17
19907
by: Nollie | last post by:
Say you have a struct: struct MYSTRUCT { int x; int y; int w; int h; };
3
2635
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 value returned from a fontdialog box, I want to update the values of the struct. I have included a code snippet below which I think will make my
10
4776
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 structures are read only) Ideally one should be able to put the redundant information there automatically so no mistakes are possible, but in a lot of case I see no way how to do it.
4
6007
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
2097
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
2576
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
2282
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
10535
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 = 100; unionMember.union1.field2 = 200 }; fullStructType var2 = { { unionMember.union2 = 300 } , { unionMember.union2 = 400 } };
13
2331
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 initialization takes place." Does this mean all non-local objects will be zero-initialized before they are initialized by their initializers(if they have)? For example: int g_var = 3; int main() {}
27
2514
by: Nate Eldredge | last post by:
Consider the following pseudo-code: #include <opaque.h> struct foo { int a; opaque_t op; int b; };
0
8486
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8931
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8828
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8680
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5705
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.