2

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;
                   }
           }
     }
3
  • What are the data types for i, j, a, k? Commented Sep 12, 2011 at 7:58
  • 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' Commented Sep 12, 2011 at 10:59
  • don't use magic numbers "j=3-1" Commented Sep 12, 2011 at 13:25

2 Answers 2

5

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.

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

1 Comment

+1: You accessing a 2D array as if it were a 1D array. Doing this gives you a pointer rather than an element of the array.
1

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.

Comments

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.