PDA

View Full Version : Pointer which can point to any variable type, in C?



crazyfuturamanoob
December 22nd, 2008, 07:08 PM
As I know, a pointer is a variable which contains a memory address.

And every variable has an address. The addresses don't differ by size,
so why there couldn't be a pointer which can point to ANYTHING?

I just want a pointer which can point to either integer, a struct, NULL, or everything.


Thanks.

dwhitney67
December 22nd, 2008, 07:11 PM
Use a void*.

Now, I do not recommend that you use void* in lieu of a pointer to an actual data type, if you know the type ahead of time, but in certain cases using void* is helpful.

P.S. When using a void*, you will not be able to perform arithmetic operations on the pointer.

crazyfuturamanoob
December 22nd, 2008, 07:25 PM
Use a void*.

Now, I do not recommend that you use void* in lieu of a pointer to an actual data type, if you know the type ahead of time, but in certain cases using void* is helpful.

P.S. When using a void*, you will not be able to perform arithmetic operations on the pointer.

Arimetric operations?

Please no complicated explanations, I'm bad at maths.

dwhitney67
December 22nd, 2008, 07:37 PM
Maybe this example program will help you understand:


#include <stdio.h>

int main()
{
int array[5] = { 1, 2, 3, 4, 5 };

int* pa = array;

for (int i = 0; i < 5; ++i, ++pa)
{
printf("%d ", *pa);
}
printf("\n");


void* va = array; // legal statement

// but can't use arithmetic operation with va, nor can it be
// accessed as if an array
/*
for (int i = 0; i < 5; ++i, ++va)
{
printf("%d ", *va);
}
for (int i = 0; i < 5; ++i)
{
printf("%d ", (int)va[i]);
}
printf("\n");
*/

return 0;
}

Npl
December 22nd, 2008, 08:45 PM
Arimetric operations?

Please no complicated explanations, I'm bad at maths.Hope you arent offended, but I find this answer funny :)

it means merely you cant add/substract from a void pointer, you can add/substract with other pointers

int array[5]={10,20,30,40,50};
int *ptr = array;
int i;

i = *(ptr + 0); // same as i = ptr[0];
ptr += 2; // same as ptr = &ptr[2];
i = *ptr; // i is now = 30


For that to work the compiler needs to know the size of an integer, 4 bytes in most cases. void has no size, thus it wont work