Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid initializer error with struct

Tags:

c

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?

like image 247
Nayef Avatar asked Sep 10 '26 05:09

Nayef


1 Answers

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)?

like image 189
Jens Avatar answered Sep 12 '26 22:09

Jens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!