4

Note: This is a homework question.

Use FOR construction to fill 2D board with values that were given by user. The program asks for board size n, m and then it asks for each board value.

My try

#include <stdio.h>
int main(){
    printf("Enter the number of columns");
    int i = scanf("%d",&i);
    printf("Enter the number of rows");
    int y = scanf("%d",&y);

    int r[i][y];
    int a;
    int b;
    for (a=0; a<i; a++){
        for(b=0; b<y; b++){
            int r[a][b] = scanf("%d",&a,&b); //bug
        }
    }
}

Bug: c:13 variable-sized object may not be initialized

EDIT:

#include <stdio.h>

int main(){

    printf("Enter the number of columns");
    int i; 
    scanf("%d", &i);
    printf("Enter the number of rows");
    int y; 
    scanf("%d", &y);

    int r[i][y];
    int a;
    int b;

        for (a=0; a<i; a++){
            for (b=0; b<y; b++){
    scanf("%d",&r[a][b]);
        }
    }
}
1
  • 2
    Please, don't ask another question by editing your first question! This makes SO hard to read! Commented Nov 21, 2011 at 15:28

5 Answers 5

7

scanf takes the address of the variable that is being read and returns the number of items read. It does not return the value read.

Replace

int i = scanf("%d",&i);
int y = scanf("%d",&y);

by

scanf("%d",&i);
scanf("%d",&y);

and

int r[a][b] = scanf("%d",&a,&b);

by

scanf("%d",&r[a][b]);

EDIT:

You are using variable length array (VLA) in your program:

int r[i][y];

as i and y are not constants and are variables. VLA are a C99 standard feature.

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

2 Comments

That will fix the scanf parts, but there is still the memory allocation to fix.
@jv42 there isn't see my comments on other answers
2

you have to allocate the 2D array dynamically cause you don't know it size in compilation.

replace

int r[i][y];

with

int *r = malloc(i*y*sizeof(int));

and when finish, add:

free(r);

*and also the SCANF errors, people already answered here.

3 Comments

This was true 12 years ago, C has supported VLA for quite some time. :)
Apparently so! :) One of those features which C has and C++ doesn't (well, it does with GCC extensions, but that non-standard).
ha... well I'm a standard kind of guy (-:
1

First the return value of scanf isn't the value that was read from stdin, instead it is the number of input values scanf read.

Secondly, C does not allow creation of arrays by using variables. You have to first create one array by dynamically allocating it. And for each entry in the first array you have to create yet another array.

Don't forget to free the memory you allocate!

3 Comments

" C does not allow creation of arrays by using variables." It does! (since C99 standard)
@Kos Sorry, you're right. Next weekend I'm printing out the standard and have it as my bed-side book!
Oh come on, just print yourself a "what's new in C99" blog post. :)
1

The use of scanf(%d, &var) is incorrect.
scanf reads from console an integer (this type is specified by its first paramenter %d) and stores it in the second parameter.
This second parameter must be a pointer, so an & is needed when your variable is not a pointer yet.

Therefore you should correct your code in this way:

int i;    
scanf("%d",&i);

int y;
scanf("%d", &y);

And in your for loop

scanf("%d", &r[a][b]);

Comments

0

It doesn't print the message because of line buffering.

If you add \n to your strings (to start a new line), it might do what you expect:

printf("Enter the number of columns\n");

Alternatively, if you really want the user to type on the same line, you need to flush the buffer manually:

printf("Enter the number of columns");
fflush (stdout);

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.