# Thread: Initializing an array of static structures

1. ## 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. 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. 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. 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. Originally Posted by qny
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....