problem accessing a struct member

This is a discussion on problem accessing a struct member within the C Programming forums, part of the General Programming Boards category; I usually write things in C++, but I have to use C to work with this vr library, so please ...

  1. #1
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179

    problem accessing a struct member

    I usually write things in C++, but I have to use C to work with this vr library, so please excuse the stupid question. I have a bunch of data allocated like this:
    Code:
    #define MAXWALLS 1000       //maximum number of walls
    
    typedef struct {
        Vec pos;
        float th, w, h;
    } Wall;
    
    typedef struct {
        enum {placingWall, rotatingWall} state;
    
        int numBalls;
        int numWalls;
    
        Vec tip;
        Vec ground;
        bool goodGround;
        
        Ball ball[MAXBALLS];
        Wall wall[MAXWALLS];
    
        vrLock lock;
    
    } WorldData;
    And at another point in my code I want to increment the th value of a wall, which I'm doing like this (expanded into multiple lines to isolate the error):
    Code:
                int index = (wd->numWalls-1) % MAXWALLS;
                Wall* wall = &wd->wall[index];
                wall->th += WALLROTATESPEED * deltaTime;   <- this line generates the error
                /*wd->wall[(wd->numWalls-1)%MAXWALLS].th += WALLROTATESPEED * deltaTime;*/
    The problem is that when I try to compile this with cc, I get this error:
    mp2sim.c:118: error: syntax error before = token
    I don't see what could be causing this. Is there some cryptic c thing going on here or am I declaring my structs wrong or what?
    Illusion and reality become impartiality and confidence.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > Wall* wall = &wd->wall[index];
    Don't you need the struct keyword:
    Code:
                struct Wall* wall = &wd->wall[index];

  3. #3
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    Doing that just gives me a more elaborate error:
    mp2sim.c:117: warning: initialization from incompatible pointer type
    mp2sim.c:118: error: dereferencing pointer to incomplete type
    mp2sim.c:118: error: syntax error before = token
    Illusion and reality become impartiality and confidence.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    See if you get an error if you don't use a pointer:
    Code:
                Wall wall = wd->wall[index];
                wall.th += WALLROTATESPEED * deltaTime;   <- this line generates the error

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Your code compiles fine for me (it is a C++ compiler though).
    Check if you have some conflicting macro. Or try to remove part by part until it compiles (meaning the last action removed the error).

    It's been a long while since I programmed in pure C, perhaps it's an old compiler that doesn't allow some construct (like += operator?) or something...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    you can't declare your variable mid-function. Go to the beginning of the function and declare your variable first: Wall* wall;

    In C all your variables must be declared before use.
    Last edited by Darryl; 03-09-2006 at 11:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. problem with struct?
    By cangel in forum C Programming
    Replies: 8
    Last Post: 09-28-2008, 12:35 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Replies: 4
    Last Post: 12-12-2002, 02:32 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21