Thread: Initializing an array of static structures

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105

    Initializing an array of static structures

    Hello friends, I am just trying to represent a grid by using a collection of node structures. These node structures contain the index locations (x,y) and two characters about the linkage of the node to other node. Well thats it, now the problem I am facing is I a am getting error in the initializing lines of the structures. I am intializing the nodes inside two for loops so that they are indexed according to the locations. The error shows as "expected expression before '{' token"

    The program is pasted here:

    Pastecode

    Please let me know where the mistake is...

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    You can post your code in the forum:
    Code:
        #include<stdio.h>
        #define N 4
        int main()
        {
            struct node{
                int x,y;
                char right,up;
            };
            static struct node grid[N][N];
            int i,j;
            for(i=0;i<N;i++){
                for(j=0;j<N;j++){
                    if(i==N-1 || j==N-1){
                        grid[i][j]={i,j,'d','d'}; /* ASSIGNMENT, NOT INITIALIZATION */
                    }
                    else{
                        grid[i][j]={i,j,'c','c'}; /* ASSIGNMENT, NOT INITIALIZATION */
                    }
                    printf("\n%d %d %d %d",grid[i][j].x,grid[i][j].y,grid[i][j].right,grid[i][j].up);
                }
            }
            return 0;
        }
    You cannot use the initialization syntax for assignment.
    You need to assign each element by itself:
    Code:
    grid[i][j].x = i;
    grid[i][j].y = j;
    grid[i][j].right = 'd';
    grid[i][j].up = 'd';

  3. #3
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Yeah qny, you are quite right...I could have posted in the forum and I usually do, but there was some problem when I was trying to post so at last I put it in pastecode. Anyway now I see that there is no error. Thanks for the correction.

    Well i was trying to initialize it that way because its shown in my book that arrays of structure could be declared and initialized that way simultaneously, although it doesn't use any loop or variables but directly during declaring according to the structure.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Some terminology: you can only initialize a variable once, when you define it. All other times it changes value is through assignment.

    Code:
    struct node array1[2] = {{0, 1, 'a', 'b'}, {2, 3, 'c', 'd'}}; /* (DEFINITION AND) INITIALIZATION */
    struct node array2[4] = {{4, 5, 'e', 'f'}}; /* (DEFINITION AND) INITIALIZATION; 3 elements are initialized with zeroes */
    array2[2] = array1[1]; /* ASSIGNMENT to 3rd element (it now has {2, 3, 'c', 'd'}) */
    array2[3].x = array2[3].y = 42; /* ASSIGNMENTs */

  5. #5
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by qny View Post
    Some terminology: you can only initialize a variable once, when you define it. All other times it changes value is through assignment.
    Thanks for the terminology....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializing a static string
    By juice in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2012, 11:04 AM
  2. initializing nested array of structures
    By leq in forum C Programming
    Replies: 3
    Last Post: 10-26-2010, 06:15 PM
  3. initializing static stdlib containers
    By skewray in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2008, 07:23 PM
  4. initializing static members
    By steve1_rm in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2008, 05:45 AM
  5. Initializing array of structures causing bus error
    By Aaron M in forum C Programming
    Replies: 5
    Last Post: 03-05-2006, 01:40 AM