struct initialization

J

John Smith

Is this a no-no? My compiler doesn't like it.

typedef struct cplex {
double real;
double imag;
} cplex;

int main(void)
{
cplex a;

a = {.5, 14.134725};

...

return 0;
}

Compiler has no problem when declaration
and initialization are combined:

cplex a = {.5, 14.134725};
 
M

Martin Ambuhl

John said:
Is this a no-no? My compiler doesn't like it.

It is a no-no; your compiler ought not like it.
typedef struct cplex {
double real;
double imag;
} cplex;

int main(void)
{
cplex a;

a = {.5, 14.134725};

...

return 0;
> }

/* WARNING: This will not work with C89 (C90). If you have a C99
compiler (or gcc) then it is also pointless, since you will have complex
types already. */

typedef struct cplex
{
double real;
double imag;
} cplex;

int main(void)
{
cplex a;
a = (cplex) { .5, 14.134725};
return 0;
}
 
S

Skarmander

John said:
Is this a no-no? My compiler doesn't like it.

typedef struct cplex {
double real;
double imag;
} cplex;

int main(void)
{
cplex a;

a = {.5, 14.134725};
<snip>
Struct literals are not supported in C90. In C99 they are, but you must
use special syntax:

a = (cplex) {.5, 14.134725};

But then, in C99 you have built-in complex types and you don't need cplex.

S.
 
A

Alexei A. Frounze

John Smith said:
Is this a no-no? My compiler doesn't like it.

typedef struct cplex {
double real;
double imag;
} cplex;

int main(void)
{
cplex a;

a = {.5, 14.134725};

...

return 0;
}

Compiler has no problem when declaration
and initialization are combined:

cplex a = {.5, 14.134725};

The following compiles with gcc w/o warnings (I use -Wall) and seems to
work, but I'm not sure of its correctness according to the C standard:

#include <stdio.h>

typedef struct ts {
long a;
short b;
} ts;

ts s1 = {0, 1};
ts s2;

int main()
{
s2 = (struct ts){2, 3};
printf ("%ld,%d\n", s1.a, (int)s1.b);
printf ("%ld,%d\n", s2.a, (int)s2.b);
return 0;
}

Old Borland C/C++ compiler for DOS doesn't like it though. I can't find a
place in the standard saying something about these things. I've seen
something before, but can't remember what and where...

Alex
 
S

Skarmander

Skarmander said:
<snip>
Struct literals are not supported in C90.

Interestingly, gcc requires -pedantic to merely warn about this in C90
mode. But then, relying on your compiler to judge whether code is
correct is a bad idea anyway. :)

S.
 
M

Mike Wahler

John Smith said:
Is this a no-no?

Yes-yes. :)
My compiler doesn't like it.

It shouldn't.
typedef struct cplex {
double real;
double imag;
} cplex;

int main(void)
{
cplex a;

a = {.5, 14.134725};

This is not initialization, but (an attempt at)
assignment. Assignment is not initialization.
return 0;
}

Compiler has no problem when declaration
and initialization are combined:

cplex a = {.5, 14.134725};

Right. This is initialization.

I believe C99 supports what you're trying to do,
but I'm not familiar with the details.

-Mike
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top