Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Initialize a union?

Posted on 2004-04-18
6
Medium Priority
?
1,673 Views
Last Modified: 2012-06-27
I am building a small library to do some 4d math for graphics/physics simulation. It is designed to be optimized for the altivec SIMD platform (mostly mac G4/G5).

I am trying to build a simple data type that I can use for both vector and non-vector operations, as my library evolves.

The altivec stuff is not important for this question. There is a base type called "vector float" that is a 128 bit variable, that represents 4 floats.

The defenition of my union (for a 4x4 matrix is):

typedef union {
   struct {
        vector float c0, c1, c2, c3;
    };
    float c[4][4];
    float e[16];
} AVMatrix4;

Is there any way to initialize this union with 4 vector floats.

I try:

vector float vf = {1.0f, 1.0f, 1.0f, 1.0f};
AVVector4 v = {{vf, vf, vf, vf}};

and gcc gives me "warning: aggregate has a partly bracketed initializer"
0
Comment
Question by:jukes1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10856162
You can initialize the *first* member of the union
AVMatrix4 v = {{1.0f, 1.0f, 1.0f, 1.0f}};

(see http://www.eskimo.com/~scs/C-faq/q2.20.html)
0
 
LVL 4

Expert Comment

by:booki
ID: 10856270
jukes1,

To add to what Mercantilum has posted, you can also do:

vector float vf = {1.0f, 1.0f, 1.0f, 1.0f};
AVMatrix4 v = {{vf, vf, vf, vf}};

*Note: Your union is not AVVector4 but AVMatrix4.
>> AVVector4 v = {{vf, vf, vf, vf}};

b.
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 600 total points
ID: 10856681
>gcc gives me "warning: aggregate has a partly bracketed initializer"

this works fine for me ... gcc 2.96

#include <stdio.h>

typedef struct
{
        float a,b,c,d;
}vector_float;

typedef union {
           struct {
                   vector_float c0, c1, c2, c3;
                  };
            float c[4][4];
            float e[16];
} AVMatrix4;

int main ()
{
        vector_float vf = {1.0f, 1.0f, 1.0f, 1.0f};
        AVMatrix4 v = {{vf, vf, vf, vf}};
}

I think the problem is "Note: Your union is not AVVector4 but AVMatrix4." as booki noted or may be you tracked the warning down to wrong data type
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:jukes1
ID: 10864424
booki,

You're right, its a AVMatrix4.

Sunnycoder, your code fails to compile with gcc 3.3 and my default compiler flags.

It gets a little more specific though. It still throws the aggregate warning, but it also gives me the error:

"error: cannot convert `vector_float' to `float' in initialization"

I figured out what the problem is. I am trying to initialize the structure in a C++ implementation file. When used in a C file, there are no errors or warnings.

I do not do a whole lot of mixing the languages. Can someone explain how to include and initialize the "C-style" structure in a .cpp file?
0
 
LVL 4

Assisted Solution

by:booki
booki earned 1400 total points
ID: 10865614
jukes1,

Problem:

    C++ does not allow for anonymous structures (no tag and no object or typedef name).  Some compilers however allow it (but only as a member of a union) as an extension to the C++ language.  The standard states that "the brace shall only contain an initializer for the first member of the union."  g++, with its extension (allowing anonymous structs in unions), implements this as the "first named member".  That is, with g++, the initializer of a union initializes the first named member.

    In AVMatrix4, the first named member is a 2-D, 4x4 array of floats 'c'.  The statement:

>>  AVMatrix4 v = {{vf, vf, vf, vf}};

therfore attempts to initialize the first row (c[0][0] ~ c[0][3]) of floats with vector_floats. Hence the error.

Solution:

Name the member.  This should compile in C++.

#include <stdio.h>

typedef struct
{
        float a,b,c,d;
}vector_float;

typedef union {
           struct {
                   vector_float c0, c1, c2, c3;
                  } a;
            float c[4][4];
            float e[16];
} AVMatrix4;

int main ()
{
        vector_float vf = {1.0f, 1.0f, 1.0f, 1.0f};
        AVMatrix4 v = {{vf, vf, vf, vf}};
}

b.
0
 

Author Comment

by:jukes1
ID: 10873124
Ok, that clears up a lot. I kept reading about the gcc unnamed union/structure extension, but missed the fact that initializing a union only initializes the first NAMED member, and not the first UNNAMED member.

Unfortunately, naming the member makes the union too annoying to use. Really the only reason I was trying to figure this out is because I am trying to design these couple of math classes to be optomized on my mac, and the easiest UI language to write in is objective-C.

I would prefer to just use c++ classes for this stuff, but you can't store a c++ class directly in an Objective-C structure, you can only store a pointer, and I don't like the syntax that this causes, and construction/destruction is a bit strange.

Thanks for all of the help people.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

722 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