-1

I'm trying to compare data in a 2D array that represents a matrix. As it is a sparse matrix, I created the 2D array compactMatrix, that has the lines and columns where the value matrix entrance is not null.

I'm using this code to make the comparison:

    if(compactMatrix[0][i] == compactMatrix[1][k] &&
       compactMatrix[1][i] == compactMatrix[0][k]){

        Do stuff...
    }

Where i and k are the indexes I'm currently looking at in a forloop. For you to know, I debugged and the value of compactMatrix[0][i] and compactMatrix[1][k]and compactMatrix[1][i] and compactMatrix[0][k] were indeed equal.

Tried to print the value using

    j = compactMatrix[0][i];
    l = compactMatrix[1][i];
    printf("%i %i", &j, &l);

but it gives me the pointer address, I guess.

So, I want to know why is it returning false in that if clause and how to fix it properly.

Rafael Kehl
  • 113
  • 2
  • 3
  • 11

1 Answers1

1

printf is used for just outputting the value it doesn't need an address. Also when you try to print an address in integral formats its an undefined behaviour . The standard requires that you print address with %p.

As CS Pei mentioned in the comments What you need probably is this:

printf ("%i %i",j,l);// you can use %d and %i interchangeably.

Some extra note about %p from the standard itself :

The corresponding argument shall be a pointer to apointer to void. The input item is converted to a pointer value in an implementation-defined manner. If the input item is a value converted earlier during the same program execution, the pointer that results shall compare equal to that value; otherwise the behavior of the %p conversion is undefined.

So if you do use p specifier don't forget to cast the argument to void*

0decimal0
  • 3,828
  • 2
  • 22
  • 38
  • This is not a answer to my question. And I don't want to print the addres, I tried to print the content just to verify AGAIN that the IF CLAUSE should work, but it's not. – Rafael Kehl Oct 01 '17 at 16:40
  • I don't understand , you yourself just said that the elements of the arrays were indeed equal. The only problem which we could find in the above described question is the problem with printing – 0decimal0 Oct 01 '17 at 18:51
  • Did you read it all? "So, I want to know why is it returning false in that if clause and how to fix it properly.". It is returning false when it should be returning true. – Rafael Kehl Oct 02 '17 at 01:10