Solved

Initialize a union?

Posted on 2004-04-18
6
1,650 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
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 150 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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 350 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

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c language help - file paths 7 149
Concatenate two strings Last and First Name 10 60
gdb doesn't stop on breakpoint 2 93
Windows Server 2012  Backup - HyperV host 18 59
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

840 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