22

if I have

int a= 5;
long b= 10;
int count0 = 2;
void ** args0;
args0 = (void **)malloc(count0 * sizeof(void *));
args0[0] = (void *)&a;
args0[1] = (void *)&b;

how can I convert from args[0] and args0[1] back to int and long? for example

int c=(something im missing)args0[0]
long d=(something im missing)args1[0]
Veverke
  • 9,208
  • 4
  • 51
  • 95
w31
  • 265
  • 1
  • 2
  • 5
  • 3
    Just as a side remark, I think it is no good practice to cast `void*` to `int` or `long`. The width of integers and of pointers need not necessarily be the same on a given platform. A more portable way is to use `intptr_t` or even better `uintptr_t`. This guarantees that you don't loose bits. – Jens Gustedt Jul 08 '10 at 06:08

6 Answers6

23

Assuming that your &a0 and &b0 are supposed to be &a and &b, and that you mean args0[1] for setting up long d, you have stored a pointer to a in args0[0] and a pointer to b in args0[1]. This means you need to convert them to the correct pointer types.

int c = *((int *)args0[0]);
int d = *((long *)args0[1]);
Arthur Shipkowski
  • 3,606
  • 1
  • 22
  • 30
5

To literally answer your question, you'd write

int c = *((int *)args0[0]);
long d = *((long *)args[1]);

What might concern me about your code is that you have allocated space for the pointers to your locations, but you haven't allocated memory for the values themselves. If you expect to persist these locations beyond the local scope, you have to do something like:

int *al = malloc(sizeof(int));
long *bl = malloc(sizeof(long));
*al = a;
*bl = b;
void **args0 = malloc(2 * sizeof(void *));
args0[0] = al;
args0[1] = bl;
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
keithm
  • 2,813
  • 3
  • 31
  • 38
0

Try this:

 int c =  *( (int *)  args0[0]);

 long d = *( (long *) args0[1]);
bits
  • 8,110
  • 8
  • 46
  • 55
  • int c=*((int )args0[0]); causes error: invalid type argument of 'unary *' (have 'int') and int c=((int *)args0[0]); fprintf(stderr,"main(): %d \n",c); causes the output to be -4261808 – w31 Jul 08 '10 at 02:37
  • I don't get it. What is the difference between my answer and the one on the top (which is accepted)? – bits Jul 08 '10 at 03:00
  • When you first posted, the "*" was missing after 'int' for some reason, perhaps due to it being interpreted as formatting -- see w31's comment for how it looked. – Arthur Shipkowski Jul 09 '10 at 16:11
0

You need to tell it that the void* should be interpreted as an int* or long* when you dereference.

int a = 5;
long b = 10;
void *args[2];
args[0] = &a;
args[1] = &b;

int c = *(int*)args[0];
long d = *(long*)args[1];
Maister
  • 4,978
  • 1
  • 31
  • 34
0

While others have answered your question, I will make a comment about the last three lines in the first part of your code snippet:

args0 = (void **)malloc(count0 * sizeof(void *));
args0[0] = (void *)&a;
args0[1] = (void *)&b;

The above is better written as:

args0 = malloc(count0 * sizeof *args0);
args0[0] = &a;
args0[1] = &b;

The malloc() call is easier to read this way, and less error-prone. You don't need a cast in the last two statements since C guarantees conversions to and from an object pointer and a void pointer.

Alok Singhal
  • 93,253
  • 21
  • 125
  • 158
0

If you're testing, i suggest use it an external function, to get more readability:

int get_int(void* value){
    return *((int*) value);
}

long get_long(void* value){
    return *((long*) value);
}

then in your code:

 int c =  get_int(args0[0]);

 long d = get_long(args0[1]);

That should work.

Nick Cuevas
  • 1,474
  • 15
  • 10