0

I have a pointer pointing to a 2D array of ints with dimensions [N][M] with M even. I define it allocating the memory as follows:

//alocate the memory of indxs pointer containing the indexs of each matrix element 

int **indxs;
indxs = (int**) malloc(N*sizeof(int *));
for(i = 0; i<N; i++){
    indxs[i] = (int *) malloc(M*sizeof(int));
}

Once filled I want to compare for each row (i_N) if the first half of the elements indxs[i_N][:M/2] is equal to the second half of the elements indxs[i_N][M/2:]. This [:M/2] is Python notation, I don't know how to do it in C, Any suggestion?. I have read in another post that for comparing the content of two pointers you have to dereference them first:

int *a = something; 
int *b = something;
*a==*b

But how can this be done in my case?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Kilian A.G.
  • 123
  • 1
  • 6
  • Usual warnig: [never cast the result of `malloc()`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Quentin Nov 04 '16 at 14:38

2 Answers2

2

First of all, this is not a 2D array but a lookup-table.

You can compare rows in your lookup-table like this:

#include <string.h>

if( memcmp( &indxs[i_N][0], 
            &indxs[i_N][M/2], 
            (M/2)*sizeof(indxs[0][0])) == 0 )
{
  puts("equal");
}

This works fine since each "row" is pointing at a real array. An array = something that has memory cells allocated in adjacent memory.

However, since you don't have a 2D array, you wouldn't be able to do this on "columns", because all your row allocations are segmented all over the heap. For example, memcmp(indxs[0], indxs[n/2], (n/2)*sizeof(indxs[0][0])) would crash and burn. Solve this by using a 2D array instead.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Thank you for your response. I have another question. I don't need to compare columns, but Do you think this is a "bad practice"? Should I use a 2D array instead? – Kilian A.G. Nov 04 '16 at 12:49
  • @KilianArteagaGutierrez The only time it makes sense to use "a pointer to array of pointers" look-up table is when you need each row to have individual length (like a dynamic string table). Then there's no way to avoid it. In all other cases, where you simply want a matrix of size [n][m], then lookup-tables are indeed bad practice and should never be used. I've written lots about this before on SO, for example [here](http://stackoverflow.com/a/32050859/584518). – Lundin Nov 04 '16 at 12:59
0

Once filled I want to compare for each row (i_N) if the first half of the elements indxs[i_N][:M/2] is equal to the second half of the elements indxs[i_N][M/2:]. This [:M/2] is Python notation, I don't know how to do it in C

You can use memcmp, which in this case may look like this:

memcmp( indxs[i_N][0], indxs[i_N][M/2], (M/2)*sizeof( int ) )

Note memcmp returns 0 if equal. Check out memcpy prototype:

   #include <string.h>

   int memcmp(const void *s1, const void *s2, size_t n);

Note also, you should get rid of the cast for malloc:

indxs = malloc(N*sizeof(int *));
artm
  • 17,291
  • 6
  • 38
  • 54