0

The program's objective is to manipulate data inside a 2D-array.

int main(void)
{
    int array[500][500];
    int x0, y0;
    if ( input(array, &x0, &y0)==0 )
        return 1;

    return 0;
}
int input( int array, int *x0, int *y0 ) {
    int x = 0;
    int y = 0;
    int err = 0;
    char c;
    printf("Enter sizes:\n");
    if ( scanf("%d %d", x0, y0)!=2 || *x0<=0 || *y0<=0 || *x0>500 || *y0>500 ) {
        printf("Invalid input.\n");
        return 0;
    }
    while ( y < *y0 ) { 
        x = 0;
        while ( x < *x0 ) { //reading entries in x coordinate
            c = getchar();

            switch(c) {
            case 'o' : array[y][x] = 1; break;
            case '.' : array[y][x] = 0; break;
            case '!' : array[y][x] = 2; break;
            default : err = 1; break;
            }
            x++;
        }
        if( x!=*x0 )
            err=1;
        y++; //move to another "line"
    }
    if (y!=*y0)
        err=1;
    if (err==1) {
        printf("Invalid input.\n");
        return 0;
    }

    return 1;
}

The program will firstly get dimensions of the array and then get one of the three letters: o, ! or . for example:

3 3

ooo

.!o

...

The problem is that my function doesn't accept its arguments: input(array, &x0, &y0) which leads to me being unable to write into that array in the switch. In the former case passing argument 1 of 'input' makes integer from pointer without a cast while in the latter subscripted value is neither array nor pointer nor vector.

My previous program used a 1D array and this way of passing arrays into functions worked.

So how should I pass my array as an argument into functions? Thanks in advance.

0

1 Answer 1

1

Move the code for the main function after that for the input function so the prototype for input is known when compiling main.

Change the prototype for input to this:

int input(int array[500][500], int *x0, int *y0) {

There are other issues with your code. For instance you do not correctly handle the line feed entered after each matrix row.

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

10 Comments

Didn't wanna write prototype since the code-part was already long. Anyway, this works. Although, why don't I have to write the size of the array in a 1D-array? Also do you have idea why the last iteration of while ( y<*y0 ) increments the y twice?
Arrays decay into pointers when passed to a function. In the case of a 2D array, a pointer to an array of arrays is passed, the second 500 is ignored and could be elided. I don't see how y would get increment twice. What makes you think that?
I printed y after increment. For 3 3 ooo 1 ooo 2 3
The line feed after the 3 3 is read by the first getchar() so the first entry is not read correctly. The same happens for each subsequent line feed is you do not enter all matrix values on a single line. The code that prints y is not what you posted, there might be other problems.
It hurts me to recommend using scanf, but scanf will ignore white space characters, including the linefeeds. So you can write char s[2]; if (scanf("%1s", s) == 1) { c = *s; }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.