Struct problem

This is a discussion on Struct problem within the C Programming forums, part of the General Programming Boards category; iv'e gone over this like 50 times and i just keep getting a 'expected primary-expression before '.' token ' error ...

  1. #1
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96

    Angry Struct problem

    iv'e gone over this like 50 times and i just keep getting a
    'expected primary-expression before '.' token ' error
    i just donno whats wrong with it :|

    Code:
    #include <stdio.h>
    
    typedef struct Sprite
    {
           int Pox, Poy;   // DEFINES CO-ORDINATES
           int Direction;    // SETS SPRITES DIRECTION
                             // 1 UP
                             // 2 RIGHT
                             // 3 DOWN
                             // 4 LEFT
           int health;       // SETS HEALTH
           int Width, Hight; // SETS SPRITE DEMENTIONS
           }Player;
    
    
           
    void func(void)
    {
    Player.Pox = 100;
    }
    
    int main(void)
    {
         func();
         
         printf("%d",Player.Pox);
             
         getchar();
    }

  2. #2
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    Why do you need the typedef? This structure works fine (as an example):
    Code:
        struct control
        {
            char caption[3];
            int x, y;
         } numberbtns;
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  3. #3
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    i thought i needed it to declear it outside of a function :S
    i must have misread something somewhere; soz i'm still learning

  4. #4
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    i tried declaring a struct in a header file, didn't work. So that would mean that structs neeed to be declared inside a function.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So the typedef means that you are making a new type called Player. It is not a variable. If you want to make a global variable, that works like master has it. If you want Player to be the name of a type, then at some point you should do
    Code:
    Player a_variable_of_type_Player;
    so that you have an actual variable to play with.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by P4R4N01D View Post
    i tried declaring a struct in a header file, didn't work. So that would mean that structs neeed to be declared inside a function.
    That's absolute bull. Structs can definitely be declared in header files, outside functions.

    QuantumPete
    Last edited by QuantumPete; 11-19-2008 at 10:43 AM.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct File and array problem Please help
    By theprogrammer in forum C Programming
    Replies: 17
    Last Post: 04-02-2009, 08:05 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  4. Replies: 22
    Last Post: 12-23-2008, 12:53 PM
  5. Replies: 16
    Last Post: 10-29-2006, 04:04 AM

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