Thread: struct + pointer help..

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    struct + pointer help..

    hmm..so i have this struct where i want to hold some user data and a value so i know if a control is enabled or disabled. then i want to have a pointer, so that i can cut down on code when changing between two sets of data. here's the code i have..
    Code:
    struct player {
           int one[2];
           int two[2];
           int three[2];
           int four[2];
           int five[2];
           int six[2];
           int toak[2];
           int foak[2];
           int fh[2];
           int ss[2];
           int ls[2];
           int yahtzee[2];
           int chance[2];
           };
    
    struct player *plyr;
    struct player pone, ptwo;
    .....
    /* test */
    void ChangePlayer(HWND hwnd)
    {
         int i = 0;
         
         for (i = 500; i <= 512; i++)
         {
             SetDlgItemInt(hwnd, i, 0, FALSE);
         }
          EnableDisable(hwnd, FALSE, 500, 512);
         
         if (whosTurn == 1)
         {
             plyr = pone;
         }
         else if (whosTurn == 2)
         {
              plyr = ptwo;
         }
         else
         {
             MessageBox(0, "I have no idea who's turn it is..", "ERROR", MB_OK | MB_ICONEXCLAMATION);
         }
         
         SetDlgItemInt(hwnd, 600, plyr.one[0], FALSE);
         if (plyr.one[1] == 1) EnableDisable(hwnd, TRUE, 500, 0);
    }
    but at plyr=pone i get an error, "incompatible types in assignment".
    could someone please tell me the error in my ways and how to get around this? i've tried googling some different combinations of pointer(s) and struct(s) but to no avail.

    also, is there a way i could initialize each variable in the first position to 0 and in the second to one "automatically"? such that pone.one[0] == 0 && pone.one[1] == 1 ? like with char array's you can just go = {0}.?
    thanks :]
    Last edited by willc0de4food; 11-02-2006 at 04:33 AM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42

    response

    try
    Code:
    plyr = &pone;

  3. #3
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    also, is there a way i could initialize each variable in the first position to 0 and in the second to one "automatically"? such that pone.one[0] == 0 && pone.one[1] == 1 ? like with char array's you can just go = {0}.?
    thanks :]
    Something like ?

    Code:
    typedef struct player
    {
        int one[2];
        int two[2];
        int three[2];
        int four[2];
        int five[2];
        int six[2];
        int toak[2];
        int foak[2];
        int fh[2];
        int ss[2];
        int ls[2];
        int yahtzee[2];
        int chance[2];
    } player;
    
    player *plyr;
    player pone = { 0,1, 0,1, 0,1, 0,1, 0,1, 0,1, 0,1, 0,1, 0,1, 0,1, 0,1, 0,1, 0,1 };
    player ptwo = { 0,1, 0,1, 0,1, 0,1, 0,1, 0,1, 0,1, 0,1, 0,1, 0,1, 0,1, 0,1, 0,1 };

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    player pone = { {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1} };
    player ptwo = pone;
    You need braces around arrays in structures when initializing their members. Also goes for structures which contain structures.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    so uh...idk if i'm bad at this game or what, but i get errors.. lol

    Code:
    struct player *plyr;
    struct player pone = { {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1} };
    struct player ptwo = pone; // <-- line 44
    ----------
    if (whosTurn == 1)
         {
             plyr = &pone;
         }
         else if (whosTurn == 2)
         {
              plyr = &ptwo;
         }
         else
         {
             MessageBox(0, "I have no idea who's turn it is..", "ERROR", MB_OK | MB_ICONEXCLAMATION);
         }
         
             SetDlgItemInt(hwnd, 600, plyr.one[0], FALSE);
             if (plyr.one[1] == 1) EnableDisable(hwnd, TRUE, 500, 0);
    ......
    errors
    yahtzee.c:44: error: initializer element is not constant

    yahtzee.c: In function `ChangePlayer':
    yahtzee.c:350: error: request for member `one' in something not a structure or union
    yahtzee.c:351: error: request for member `one' in something not a structure or union
    the problem is..? :] thx guys
    Registered Linux User #380033. Be counted: http://counter.li.org

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    typedef struct player
    {
        int one[2];
        int two[2];
        int three[2];
        int four[2];
        int five[2];
        int six[2];
        int toak[2];
        int foak[2];
        int fh[2];
        int ss[2];
        int ls[2];
        int yahtzee[2];
        int chance[2];
    } player;
    
    int main( void )
    {
        player *plyr;
        player pone = { {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1} };
        player ptwo = pone;
        
        return 0;
    }
    With exception to warnings about unused variables, this compiles without warning or error.
    Code:
    SetDlgItemInt(hwnd, 600, plyr.one[0], FALSE);
    plyr is a pointer. As such, you use the arrow operator, not the dot operator, to access its members.


    Quzah.
    Last edited by quzah; 11-03-2006 at 12:24 PM.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    struct player pone = { {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1} };
    This is an initialisation, which you can do OUTSIDE of functions, so long as all the members are constants.
    
    struct player ptwo = pone; // <-- line 44
    Just copy all the {} things
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    heh..sweet.
    it compiles now :]

    time for some testing..
    Last edited by willc0de4food; 11-03-2006 at 12:50 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer problem or so...
    By TL62 in forum C Programming
    Replies: 19
    Last Post: 01-12-2008, 11:45 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. "dereferencing pointer to incomplete type"
    By incognito54 in forum C Programming
    Replies: 2
    Last Post: 11-01-2005, 09:50 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM