Solved

Struct Declaration

Posted on 2004-03-24
8
476 Views
Last Modified: 2010-04-15
Let say we have this struct:
struct foo {
   int x;
   int y;
   int z;
};

In Linux, we can do

Case 1:
struct foo f = {
   x : 1,
   y : 2,
   z : 3,
};

Case 2:
struct foo f = {
   x : 1,
   z : 2,
   y : 3,
};  /* Note y and z are switched */

In Windows, with Case 1, I can initialize that have the same result. However, with Case 2, how can I do it when z and y are switched since Windows doesn't like
struct foo f = {
  parameter : value,
  ...........
};

Thanks!
0
Comment
Question by:beneflex
8 Comments
 
LVL 1

Expert Comment

by:beavis_shenzhen
ID: 10673577
the constructor of struct foo is nothing to do with the OS, Win or Linux.

consider:
switch(No)
{
    case 1:
    {
        struct foo f;
        f.x = 1;
        f.y = 2;
        f.z = 3;
        ...........
        break;
    }
    case 2:
    {
        struct foo f;
        f.x = 1;
        f.y = 3;
        f.z = 2;
        ...........
        break;
    }
}
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10674269
"Case 2" (switching of y and z) is not implemented in C++ (it is not the problem with windows)!
The struct under C and struct in C++ are implemented differently!
In C++, when you initialize a structure it calls a constructor (which is not the case in C) and the values of initialization are passed as parameters to this constructor and these have to be in the same order as in the constructor!

You can try something like this:
struct foo
{
   int x;
   int y;
   double z;
   foo(int a, int b, double c) { x = a; y = b; z = c;}
   foo(int a, double b, int c) { x = a; y = c; z = b;}
};

main()
{
        foo f(1, 2, 3.0), f1(1, 2.0, 3);
}


-ssnkumar
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 10674498
seems you got the answer from ssnkumar,
still just out of curiosity, why would you do that ( switching the inializer variables)..
why do you want to twist the things around? When they work absolutely fine done straight away.
that will make the code less readable for reviewers.
(may be you find it more pleasurable to hold the ear around the head ;-P)..


akshay
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 5

Expert Comment

by:dennis_george
ID: 10674645
Hi,

what you have given

struct foo f = {
   x : 1,
   y : 2,
   z : 3,
};

is memberwise assignment which is only present in linux and is not a ANSII standard (I think it might be the C99 standard).

In Windows environment or the compiler which only support ANSII you have to assign the structure value individually as shown by  beavis_shenzhen...

If you want to initialize all the variables then you can use the asignment operator

struct foo f = {1, 2, 3} ;
or
struct foo f = {1, 3, 2} ;

Hope you got it
Dennis
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 10674646
Hi,

what you have given

struct foo f = {
   x : 1,
   y : 2,
   z : 3,
};

is memberwise assignment which is only present in linux and is not a ANSII standard (I think it might be the C99 standard).

In Windows environment or the compiler which only support ANSII you have to assign the structure value individually as shown by  beavis_shenzhen...

If you want to initialize all the variables then you can use the asignment operator

struct foo f = {1, 2, 3} ;
or
struct foo f = {1, 3, 2} ;

Hope you got it
Dennis
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 10674655
Oops I entered it twice.... may be I refereshed it after i submitted ....
Sorry for that

Dennis
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 500 total points
ID: 10676294
Hi beneflex,
> struct foo f = {
>    x : 1,
>    y : 2,
>    z : 3,
> };

That's not a question of Windows or Linux, but a question of the used compiler. GCC in Linux has tons of extension, so sometimes it's not transparent why GCC jumps through the hoop and VC++ doesn't.

Try the GCC manual:
http://gcc.gnu.org/onlinedocs/gcc-3.3.3/gcc/

Cheers,

Stefan
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10676305
BTW: If you want all those fancy GCC extensions in Windows, try the cygwin environment:

http://www.cygwin.com

It comes with GCC and compiles a lot of Linux sources out-of-the-box.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Estimate porting issues. 9 120
Need to explain what is Ambiguous Permutations and why the solution works . c++ 5 149
sameEnds challenge 3 179
Test the speeds on my PC Drives 12 61
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question