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);
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 _ Commented Mar 8, 2016 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]); Commented Mar 8, 2016 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). Commented Mar 8, 2016 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. Commented Mar 8, 2016 at 13:29

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
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.
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;
Okay, you're right. we were on different context. I was talking about conversion and access. never mind.

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.