4

I am just trying to set up a simple recursive struct without too much knowledge of C (have to learn somehow)

here is my make compile line

g++ -o cs533_hw3 main.c

here is my code

typedef struct Node Node;

struct Node
{
    int texture;
    float rotation;
    Node *children[2];
};

Node rootNode;
rootNode.rotation

Here is my error on the last line

error: 'rootNode' does not name a type
Justin Giboney
  • 3,271
  • 2
  • 19
  • 18
  • 2
    [Don't use g++ for C code, use gcc.](http://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc) – Mike Mar 13 '13 at 17:18
  • +1 @Mike, except that for both `clang` or `clang++` might be even better for a beginner - for the higher quality error messages if nothing else. – Carl Norum Mar 13 '13 at 17:20

2 Answers2

20

Code has to be in functions in C. You can declare variables at the global scope, but you can't put statements there.

Corrected example:

typedef struct Node Node;

struct Node
{
    int texture;
    float rotation;
    Node *children[2];
};

Node rootNode;

int main(void)
{
    rootNode.rotation = 12.0f;
    return 0;
}
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
0

Looks right. But you probably wanted to do something with rootNode.rotation?

Node rootNode;
memset(&rootNode, 0, sizeof(rootNode)); // zero everything there
rootNode.rotation = .5f;
Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18