2
votes

I am trying to sort the elements in a 2d array ,but in assigning the value to k it is giving this error.

"warning: assignment makes integer from pointer without a cast".

Why am I getting it and how can I resolve it?

for(i=1;i<3;++i)
    {
        for(j=3-1;j>=1;--j)
           {
                if(a[j-1]>a[j])
                   {
                         k = a[j-1];
                          a[j-1]=a[j];
                         a[j] = k;
                   }
           }
     }
2
What are the data types for i, j, a, k? - Ed Heal
Whatever you do, the solution is NOT adding a cast! The warning is poorly worded ... I think it should have just said: "warning: assigning a pointer to an integer" ommiting the word 'cast' - pmg
don't use magic numbers "j=3-1" - John Retallack

2 Answers

5
votes

You have missed one index in accessing the element. a[j], a[j-1] should be a[i][j], a[i][j-1] in all places. a[i] is the pointer to the first element of i-th row in a 2D array. a[i][j] is the element at index i, j.

Note: Though not specified in the question, I am almost sure that i, j, k are integers and a is a 2D integer array.

1
votes

The warning is around unsafe conversion from pointer to integer in one of the assignment statements. It could be any of the statements in your code. You should provide the variable type declaration in order to detect where the warning comes from.