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? 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.
"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
"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
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.
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!
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.
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?
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.
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Nollie |
last post by:
Say you have a struct:
struct MYSTRUCT
{
int x;
int y;
int w;
int h;
};
|
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
|
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.
|
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;
};
|
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;
| |
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;
};
|
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}
|
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 } };
|
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() {}
|
by: Nate Eldredge |
last post by:
Consider the following pseudo-code:
#include <opaque.h>
struct foo {
int a;
opaque_t op;
int b;
};
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |