0

How can I convert a int 2d array initialized liked this:

int 2darray[9][9];

Into a void * then back to a 2d array again. This one gives me an incompatible pointer type error

int **sub = *((int **)2darray);
James Liam
  • 17
  • 1
  • 4
  • Its possible as `int darray[9][9];` `void *sub = darray;` `int **subInt = (int **)sub;` AFAIK, Variable cannot start with a number. Its always a character or $ or _ – Kishore Bandi Mar 08 '16 at 13:04
  • I'm actually passing into a pthread like this pthread_create(&column, NULL, columncheck, (void*)darray);. But it's giving me a segmentation fault when I do something like this: printf("%d ", subInt[0][0]); – James Liam Mar 08 '16 at 13:07
  • May be its not the conversion issue. Can you share the whole code to check the issue? because segmentation fault occurs due to some coredump. Could be invalid access of address (due to not initializing the pointer correctly as well). – Kishore Bandi Mar 08 '16 at 13:20
  • I've just decided to that what has worked for me before, using malloc. Patrick Trentin said I can't cast a ponter back to an array. – James Liam Mar 08 '16 at 13:29

1 Answers1

0

The cast to int** should be like this:

int **sub = ((int **)2darray);

You can not cast the pointer back to an array, it just doesn't make sense. You can, however, cast the pointer back to an array-pointer as shown here https://stackoverflow.com/a/20046703/6024122. As noted in that answer, this is quite uncommon, I've never personally witnessed it.

ETA: pthread_create requires its parameter to be a void *arg, thus you should do something like this:

pthread_create(..., (void *) 2darray);

and then

int **ptr = (int**) arg;
Community
  • 1
  • 1
Patrick Trentin
  • 7,126
  • 3
  • 23
  • 40
  • Well, I agree with the part where it may not make sense. However its valid to convert array to pointer. Internally they're the same. You can check my comment for the sample code which will compile successfully. – Kishore Bandi Mar 08 '16 at 13:11
  • Converting array to pointer does make sense, what it does not make sense is pointer to array, it's not even possible. At best you can convert it into array pointer, to the best of my knowledge. – Patrick Trentin Mar 08 '16 at 13:13
  • Sorry, I meant pointer to array itself. Its completely valid. Like I said they're treated the same at the end of the day for processing. They access the address directly and increment based on the basic bytes allocated. Example: `int a[2]; // Initialize with something.` you can `cout << *(a+1)` this is equal to `cout << a[1]`. Similarly you can assign a pointer to array to array to pointer as well. – Kishore Bandi Mar 08 '16 at 13:16
  • I am afraid we are not talking about the same thing, that's just how you resolve pointer indexing. I mean actually doing something like int a[2] = ptr; – Patrick Trentin Mar 08 '16 at 13:20
  • Okay, you're right. we were on different context. I was talking about conversion and access. never mind. – Kishore Bandi Mar 08 '16 at 13:24