1

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 :)

5
  • I think the & has higher precedence than [], so you should use scanf( "%d", &(array[i][j]) ); Commented Nov 20, 2013 at 16:20
  • 1
    I recently learned that [] has highest precedence, so I don't think so it will help. Commented Nov 20, 2013 at 16:22
  • btw, I tweaked the program and it turned out that if I use int array, there will be no problem. Still, I'd like to know why char array is giving the error? Commented Nov 20, 2013 at 16:31
  • The GNU C manual says that [] 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. Commented Nov 20, 2013 at 16:41
  • @Light maybe because you're trying to fit an int (probably 4 or 8 bytes) into a char (1 byte). Commented Nov 20, 2013 at 16:46

3 Answers 3

1

Your'e trying to put an int (%d) into a char (char array[ROW][COL]), so there's a memory violation.

Sign up to request clarification or add additional context in comments.

2 Comments

Yep, if I change the array from char to int, it makes no error; however, what if I need to use char array not any other type, should I use %c in scanf? Though, using scanf("%c", &array[i][j]) gives me wrong inputs (if I make them display) and it doesn't even complete the entire loops.
@Light There seems to be an answer here stackoverflow.com/questions/3952111/… . But I would simply store it in a temporary int and then cast it to char, for example: int aux; scanf("%d", &aux); array[i][j] = (char) aux; . Make sure to check the boundaries, though.
0

@light:

btw, I tweaked the program and it turned out that if I use int array, there will be no problem. Still, I'd like to know why char array is giving the error?

If you want to use a char array, you can use the following.

At the moment I have only access to gcc, but when playing with your code I observed the following:

#define ROW 2   // note that I defined a 2x2 matrix
#define COL 2
....
char array[ROW][COL];
....
scanf("%c", &array[i][j]);
....

I use the following lines to print the result:

printf("\n");
for (int i = 0; i < ROW; i++)
{
    for (int j = 0; j < COL; j++)
    {
        printf("%c ", array[i][j]);
    }
    printf("\n");
 }

If I want the following four values as input 1 2 3 4 I observe the following:

$./a.out
1
2    # input breaks here and the following lines are printed....

1 

2 

However, if I use the following line

do {scanf("%c",&array[i][j]);} while ( getchar() != '\n' );

of code instead of

scanf("%d", &array[i][j]);

in your two for loops it seems to work as expected.

Example input and output (the numbers are actually chars):

$./a.out
1
2
3
4

1 2  #output 
3 4 

And of course it works for letters:

$./a.out
a
b
c
d

a b #output
c d

3 Comments

hoho... actually it worked. But... i think i think... I'm doing a dumb mistake (I don't know about)... I'm inputting 1, 2 and 3 and when I print the array out I get the ASCII key codes of 1, 2 and 3. Why?
when you print the output, did you make sure that you use %c, not %d? If I enter 1 2 3 4 and use %d instead of %c I get 49 50 51 52
thanks man, now it's flawless. (I was using %d before for printing) :)
0

Declare your array as it should be declared:

int array[ROW][COL];

and this will solve your issue. %d format specifier expects pointer to int to be passed and you pass pointer to char. Your array is to small to fit values you try to put there with scanf.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.