0

I'm trying to get an input from the user and store it in a certain place in a 2d Array called Matrix. The Compiler says:

error: assignment makes integer from pointer without a cast [-Werror] Matrix[i][j]=matrixinput;

This is the part of my Program:

 int i=0;
    int j=0;
    char size[1];
    char matrixinput[1];
    int sizeint=0;
    int Matrix[10][10];

 while (i<=(sizeint-1) && j<=(sizeint-1)){

            gets(matrixinput);
            Matrix[i][j]=matrixinput;
            j++;

            if (j==(sizeint-1)){
                    j=0;
                    i++; }
            else {continue;};
    };

I am just a Student with basically 0 expeience in c or any kind of programming, so maybe somebody can help me to find out how I can fix this.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 1
    `gets(matrixinput)` is a huge blunder – M.M Nov 20 '15 at 13:39
  • 1
    [Why is the gets function so dangerous that it should not be used?](http://stackoverflow.com/q/1694036/3488231) – user12205 Nov 20 '15 at 13:42
  • especially with a buffer size of 1 – M.M Nov 20 '15 at 13:47
  • A few of the problems: 1) Please read the man pages for the system functions being called. I.E. for `gets()` Then replace with `fgets()` 2) sizeint is 0 so sizeint-1 is -1 and 'i' and 'j' are set to 0 so are NOT `<=` -1 so the loop, when the code is executed will never be entered. 3) in C, using an array name degrades to the *address* of the array 4) this: `matrixinput[1]` declares a one char array but this: `int Matrix[10][10];` is integers. Assigning a pointer to an integer causes the compiler to raise a warning. – user3629249 Nov 21 '15 at 15:44
  • do not use tabs for indenting the code because every word processor/editor has the tab widths/tab stops set differently. always use spaces. Suggest using 4 spaces as that allows many indent levels across the page and is wide enough to be visible with a variable width font. – user3629249 Nov 21 '15 at 15:46
  • for readability and understandability by us humans, this: `else {continue;};` should be: `else { continue; }` Note no trailing semicolon after *any* closing brace '}' – user3629249 Nov 21 '15 at 15:50
  • Note: in C, negative numbers are smaller than 0, so the `while()` loop will never be entered. – user3629249 Nov 21 '15 at 15:53
  • this line: `int Matrix[10][10];` declares an array of 10x10 integers. However, the `while()` loop is counting the indexes into that matrix from 0 until 4gig entries, (2gig positive entries, of which those after the first 100 will be beyond the upper bounds of the array and 2gig negative entries that will be before the beginning of the array.) writing to memory outside the bounds of the array is undefined behaviour and can/will lead to a seg fault event. – user3629249 Nov 21 '15 at 16:00

4 Answers4

2
Matrix[i][j]=matrixinput;

What you are doing is assign a char array of size 1 to an int. Your compiler issues a warning for this.

Don't assign arrays like this .

You want to take input in 2-d array, directly take inout in it using scanf -

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

And this loop -

while (i<=(sizeint-1) && j<=(sizeint-1)){

This loop won't iterate for once , because condition will remain always flase as sizeint-1 is -1 as sizeint is -1 . So , you need to work on this condition also .

Note - Increase your array's size and don't use gets , use fgets instead.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • I tried to use atoi to make an int from the char. now the Compiler tells me: lvalue required as left operand of assignment atoi(matrixinput)=matrixinput; error: assignment makes integer from pointer without a cast [-Werror] Matrix[i][j]=matrixinput; – IDKanything Nov 20 '15 at 13:32
  • @IDKanything I think you need to do `Matrix[i][j]=atoi(matixinput);` , but remember `atoi` requires string as argument , you should pass it a `char` array not terminated by `'\0'` . So , if you do this now with no changes in array , then you get another big problem . Don't do this right now . Just take input directly in array `Matrix`. – ameyCU Nov 20 '15 at 13:40
  • Thank you! I think I got it now. – IDKanything Nov 20 '15 at 13:41
  • Change `int Matrix[10][10];` to `char Matrix[10][10];`. Why are you storing characters (char) read from the input as integers (int)? They are not the same. – pedwards Nov 20 '15 at 13:41
1

A professor gave me good tip for debug this type of error, ask you if the left operator type is equals to the right operator type. in your code it's false because right operator equals integer and left operator equals character.

panic
  • 2,004
  • 2
  • 17
  • 33
  • the left operator (`Matrix[i][j]`) is an `int`, the right operator (`matrixinput`) is an `array of 1 char`. An `int` would be assignable to a `char`. – mch Nov 20 '15 at 13:56
  • @mch, True, assigning a char to an integer would be 'ok' BUT that is NOT what is being done. Rather the address of the array `matrixinput` is being assigned to an entry in the integer `Matrix[][]` – user3629249 Nov 21 '15 at 16:03
1

What you could do to remove the error is: Matrix[i][j] = (int) matrixinput[0]; (Array indexing starts with 0)

But your code will still not work because you have initialized i and j to 0 and also sizeint. It means that, in the loop while (i<=(sizeint-1) && j<=(sizeint-1)), you are basically checking if 0 <= -1 which is impossible and so your while loop will never execute. What you could do is:

gets(matrixinput);
for(i = 0; i < 10; i++)
{
    for(j = 0; j < 10; j++)
    {
        if(i == 0 && j == 0) { //say you want to put the char at the first row first column
            Matrix[i][j] = matrixinput[0];
        }
    }
}

You could also use scanf() instead of gets().

Pang
  • 9,564
  • 146
  • 81
  • 122
Vinay Madapura
  • 327
  • 5
  • 17
0

here is one possible correction to the posted code

It removes unneeded clutter from the code

It makes use of scanf() so no char to int conversion needed in the code

It consistently indents the code

It #defines the size of the matrix to make the code clearer

Note: it fails to check the returned value (not the parameter value) from scanf() to assure the input/conversion was successful

#define MATRIX_SIZE (10)
// int i=0;
// int j=0;
// char size[1];
// char matrixinput[1];
// int sizeint=0;
//int Matrix[10][10];
int Matrix[MATRIX_SIZE][MATRIX_SIZE];

// while (i<=(sizeint-1) && j<=(sizeint-1)){

//        gets(matrixinput);
//        Matrix[i][j]=matrixinput;
//        j++;

//        if (j==(sizeint-1)){
//                j=0;
//                i++; }
//        else {continue;};
//};

for( int i=0; i<MATRIX_SIZE; i++)
{
    for( int j=0; j<MATRIX_SIZE; j++)
    {
        scanf( "%d", Matrix[i][j]);
    }
}
user3629249
  • 16,402
  • 1
  • 16
  • 17