Reputation: 15
int main(int argc, char* arv[])
{
int m;
printf("How many Rows and Lines?:\n");
scanf("%d", &m);
char board[m][m] ;
int rows, columns;
for(rows = 0; rows < m; rows++)
{
for(columns = 0; columns < m; columns++)
{
char *board = malloc(rows*columns*sizeof(int));
}
}
for(rows = 0; rows < m; rows++)
{
for(columns = 0; columns < m; columns++)
{
printf("%c", board[rows][columns]);
}
printf("\n");
}
}
The Problem is that i evreytime get wrong output :/ If someone can help me it were nice, i am new at programming
Upvotes: 1
Views: 43
Reputation: 9786
As people said, your board
inside the loop is completely unrelated to the one outside, so any change performed on it won't be reflected. From that, the reason that at every time you get something different it is because the actual board
is not initialized and can take casual values.
Upvotes: 0
Reputation: 859
Do you just want to initialize the array? It's already created outside the loops and you don't need to allocate the memory again. This:
for(rows = 0; rows < m; rows++)
{
for(columns = 0; columns < m; columns++)
{
board [rows][columns]='a';
}
}
In place of the first nested loop will give this as result:
How many Rows and Lines?:
5
aaaaa
aaaaa
aaaaa
aaaaa
aaaaa
Upvotes: 2
Reputation: 35520
the board
inside the first loop has scope restricted to that loop. You are not modifying the array you declared at the beginning of the function.
What do you want to put inside the array?
Upvotes: 1