Thread: Structing Array = Errors! O..o

  1. #1
    Registered User l2krence's Avatar
    Join Date
    Nov 2010
    Posts
    24

    Red face Structing Array = Errors! O..o

    here's how i write my code of trying to construct a struct of array but ... hmmmm =,=lll
    errors basically @_@ this shows tat i weak on STRUCT ~~~ teach me pls! i always fail on making struct....

    Code:
    #include <stdio.h>
    #define ROW 3
    #define COL 3
    
    typedef struct {
    
    int arr[3][3];
    
    }board;
    
    main() {
    int x, y;
    
    for (x=0; x<ROW;x++) {
         for (y=0;y<COL;y++) {
         printf(" %");
         }
    }
    //trying to set the array of position [0][0] to '#';
    
    board arr[x][y] = board.arr[0][0] = '#';
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You made board an alias for the struct type. It is not an object. An example of what I suspect you are actually trying to do:
    Code:
    #include <stdio.h>
    
    #define ROW 3
    #define COL 3
    
    typedef struct {
        int arr[ROW][COL];
    } board;
    
    int main() {
        board my_board;
        int x, y;
    
        for (x = 0; x < ROW; x++) {
            for (y = 0; y < COL; y++) {
                my_board.arr[x][y] = '#';
            }
        }
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User l2krence's Avatar
    Join Date
    Nov 2010
    Posts
    24
    Quote Originally Posted by laserlight View Post
    You made board an alias for the struct type. It is not an object. An example of what I suspect you are actually trying to do:
    Code:
    #include <stdio.h>
    
    #define ROW 3
    #define COL 3
    
    typedef struct {
        int arr[ROW][COL];
    } board;
    
    int main() {
        board my_board;
        int x, y;
    
        for (x = 0; x < ROW; x++) {
            for (y = 0; y < COL; y++) {
                my_board.arr[x][y] = '#';
            }
        }
        return 0;
    }
    oh i see!!! that makes sense now.
    so can i set my array value of any position with what i want anytime i want them?
    like
    my_board.arr[0][1] = rand()% 45 +35;
    // i want to set position 0 , 1 to be a random number.
    is this move illegal? O..o

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by l2krence View Post
    oh i see!!! that makes sense now.
    so can i set my array value of any position with what i want anytime i want them?
    like
    my_board.arr[0][1] = rand()% 45 +35;
    Yep!
    Quote Originally Posted by l2krence View Post
    // i want to set position 0 , 1 to be a random number.
    is this move illegal? O..o
    Nah!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 03:15 PM
  2. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM