I'm writing a simple code in visual studio 2012 RC that asks the user to insert values for two-dimensional array.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROW 5
#define COL 3
int main()
{
char array[ROW][COL];
int value;
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
scanf("%d", &value);
array[i][j] = value;
// scanf("%d", &array[i][j]); //use this statement instead of above 2 statements.
}
printf("\n");
}
The problem is that the above code works fine; however, if I use (as pointed out in commented part)
scanf("%d", &array[i][j])
directly, instead of storing the input into a variable and then assigning it to the array, the program will give an error of something like 'stack memory around 'array' has corrupted'.
Could someone tell me why I'm getting this problem and how can I avoid it while storing the input directly into array, instead of storing the input in a variable first.
ps- This is my first post, so be gentle :)
&has higher precedence than[], so you should usescanf( "%d", &(array[i][j]) );[]has highest precedence, so I don't think so it will help.int array, there will be no problem. Still, I'd like to know whychar arrayis giving the error?[]has the highest precedence, so&array[i][j]should do. gnu.org/software/gnu-c-manual/… . It could be a compiler bug or a real memory access error.int(probably 4 or 8 bytes) into achar(1 byte).