Richard
Richard

Reputation: 242

Another "x" does not name a type error

Unlike a lot of the other 'x' does not name a type errors on here, I don't think this one involves circular dependencies, but I'm still having trouble figuring it out.

typedef struct        /* structure definitions */
{
   float  mat[4][4];
}  matrix_unit;

matrix_unit I = {
{ 1., 0., 0., 0.,
  0., 1., 0., 0.,
  0., 0., 1., 0.,
  0., 0., 0., 1  },
};

matrix_unit *stack[50];    /* (line 456) array of pointers to act as a stack */
matrix_unit stackbase = I;
stack[0] = &stackbase;  // 'stack' does not name a type

Since stack has already been declared as a stack of pointers to matrix_unit structs, shouldn't this be valid?

When I compile the code with "gcc -c 3D.c", I get the following errors from these lines:

3D.c:457:1: error: initializer element is not constant
3D.c:458:1: warning: data definition has no type or storage class
3D.c:458:1: error: conflicting types for ‘stack’
3D.c:456:14: note: previous declaration of ‘stack’ was here
3D.c:458:1: error: invalid initializer

Thanks in advance for the help.

Upvotes: 0

Views: 1243

Answers (1)

Hans Passant
Hans Passant

Reputation: 941635

The compiler is trying to parse line 458 as a declaration. It is not, it is a statement. Statements must be written inside a function. Like this:

void initialize() 
{
    stack[0] = &stackbase;
}

Upvotes: 2

Related Questions