Jun 17, 2012 at 4:26pm Jun 17, 2012 at 4:26pm UTC
I tried this code:
1 2 3 4 5 6
void *arr = new (int *)[num];
for (int x=0; x<num; x++)
cin>>(int *)arr[x];
delete []arr;
but I get errors
what the problem ?
Last edited on Jun 17, 2012 at 4:27pm Jun 17, 2012 at 4:27pm UTC
Jun 17, 2012 at 4:43pm Jun 17, 2012 at 4:43pm UTC
AFAIK, void pointer can only be used as a parameter or as a return value of a function...
CMIIW
Jun 17, 2012 at 4:49pm Jun 17, 2012 at 4:49pm UTC
You're trying to read something into a pointer (a temporary pointer, no less), which makes no sense (nor does any other part of the example).
AFAIK, void pointer can only be used as a parameter or as a return value of a function...
There's nothing (syntactically) wrong with the first line.
Of course you need a cast, however void pointers are not restricted to parameters or return values.
Last edited on Jun 17, 2012 at 5:36pm Jun 17, 2012 at 5:36pm UTC
Jun 17, 2012 at 5:27pm Jun 17, 2012 at 5:27pm UTC
@Athar
There's nothing (syntactically) wrong with the first line.
I doubt that there is nothing wrong with the syntax of the first line.
@myoni
For what type of object are you trying to allocate the memory?
Last edited on Jun 17, 2012 at 5:31pm Jun 17, 2012 at 5:31pm UTC
Jun 17, 2012 at 5:54pm Jun 17, 2012 at 5:54pm UTC
What I think you're trying to do is allocate a variable size array of ints and then enter a number into each entry in the array.
Try the following:
1 2 3 4
int * arr = new int [NUM];
for (int x=0; x<NUM; x++)
cin>>arr[x];
Last edited on Jun 17, 2012 at 6:14pm Jun 17, 2012 at 6:14pm UTC
Jun 17, 2012 at 6:25pm Jun 17, 2012 at 6:25pm UTC
I need to use void*, because It's need to be like template
to use in int, char, float etc...
Jun 17, 2012 at 6:26pm Jun 17, 2012 at 6:26pm UTC
vlad,
I need it for exercise