Nayef
Nayef

Reputation: 374

Invalid initializer error with struct

I have two structures one is basic, position_3d. The other one is ray.

typedef struct{
float x,y,z;    
} position_3d;

typedef struct{
vector_3d direction;
position_3d startPosition;  
} ray;

I have implemented a function which return a position_3d struct

position_3d getIntersectionPosition(ray r, sphere s){
    position_3d pos; 
    //some code

    pos.x = r.startPosition.x + t*r.direction.x; 
    pos.y = r.startPosition.y + t*r.direction.y; 
    pos.z = r.startPosition.z + t*r.direction.z;  
    return pos; 
}

when I call

position_3d pos = getIntersectionPosition(r, s);

I got this error invalid initializer. I am using gcc. The compilation command is gcc prog.c -o prog.out -lm -lGL -lGLU -lglut

I am really stuck now! May anybody help?

Upvotes: 3

Views: 8051

Answers (3)

Nayef
Nayef

Reputation: 374

The problem was because of improper order of functions. I mean I I need to define the signature of all functions before calling them.

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 755006

There are a lot of gaps to fill in before we can see whether the code should be compilable:

  • Type sphere?
  • Type vector_3d?
  • Variable t?

When we come up with plausible-looking hypotheses for these types, as in this code (file sphere.c):

typedef struct
{
    float x,y,z;
} position_3d;

typedef struct
{
    float x, y, z;
} vector_3d;

 typedef struct
{
    vector_3d direction;
    position_3d startPosition;
} ray;

typedef struct
{
    float radius;
    position_3d centre;
} sphere;

position_3d getIntersectionPosition(ray r, sphere s)
{
    position_3d pos;
    float t = s.radius;

    pos.x = r.startPosition.x + t*r.direction.x;
    pos.y = r.startPosition.y + t*r.direction.y;
    pos.z = r.startPosition.z + t*r.direction.z;
    return pos;
}

position_3d func(ray r, sphere s)
{
    position_3d pos = getIntersectionPosition(r, s);
    return pos;
}

Then I can compile using:

$ /usr/bin/gcc -O3 -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
       -Wold-style-definition -c sphere.c
sphere.c:24: warning: no previous prototype for ‘getIntersectionPosition’
sphere.c:35: warning: no previous prototype for ‘func’
$

These warnings are correct; they are prompted by the -Wmissing-prototypes options. Normally, there'd be a header declaring the types and the functions.

Thus, by demonstration, the code could be correct if written properly. In C, you cannot modify the code so that it reads:

ray         r;
sphere      s;
position_3d pos = getIntersectionPosition(r, s);

position_3d func(void)
{
    return pos;
}

That then gives a warning:

sphere.c:43: error: initializer element is not constant

If you compile with g++ instead of gcc, though (and remove the C-specific options, leaving):

$ g++ -O3 -g -Wall -Wextra -c sphere.c
$

Then it compiles cleanly — a demonstration, once more, that C and C++ are not the same languages, not that you were claiming this.

The error I get isn't the error you claim to be getting. That means I've not managed to guess accurately what you are doing in your code. So, please:

  • Modify this example code until it reproduces the error you are getting. Note that the compilation needs no headers, no libraries (we only need to get to an object file, so libraries are immaterial; you aren't using anything except standard headers, it seems (at most), but cut out all the headers related to GL). Headers from the C standard or POSIX standard are permitted, but should not be necessary.
  • Add your modification of this code to the end of your question (leaving the existing material unchanged).
  • Show the exact compiler warnings you get under this stringent set of C compilation options. (That means there should be at least two warnings — for the lack of previous prototypes. But there should also be an error, the one you are claiming prevents your code from working.) We should be able to correlate the line numbers in your error messages trivially to the code you show.

Upvotes: 1

Jens
Jens

Reputation: 72746

Is the line

position_3d pos = getIntersectionPosition(r, s);

at file scope (then that's an error because the initializer is not constant) or block scope (then it should work)?

Upvotes: 2

Related Questions