void pointer?
-
How can I store address of integer array to void pointer and print array using void pointer?
-
How can I store address of integer array to void pointer and print array using void pointer?
sam_psycho wrote:
How can I store address of integer array to void pointer
int anArray\[1234\]; void\* pointyThing = (void\*)(anArray);
and print array using void pointer?
I don't know of any built-in way to print arrays from a void*, but presumably you can write a function to do it...
MyPrintArrayFunction(pointyThing);
There are three kinds of people in the world - those who can count and those who can't...
-
sam_psycho wrote:
How can I store address of integer array to void pointer
int anArray\[1234\]; void\* pointyThing = (void\*)(anArray);
and print array using void pointer?
I don't know of any built-in way to print arrays from a void*, but presumably you can write a function to do it...
MyPrintArrayFunction(pointyThing);
There are three kinds of people in the world - those who can count and those who can't...
To print array compiler giving error: I tried following code but It is not working,
int arr[]={10,20,3};
void *ptr=(void *)arr;
printf("%d",*ptr); // getting error on this lineSo, please any suggestion to print array using void pointer, And most imp why compiler not allowing above code .. :confused:
-
To print array compiler giving error: I tried following code but It is not working,
int arr[]={10,20,3};
void *ptr=(void *)arr;
printf("%d",*ptr); // getting error on this lineSo, please any suggestion to print array using void pointer, And most imp why compiler not allowing above code .. :confused:
printf expects you to pass a value as an argument. By definition you can't resolve a void pointer. Be happy; in the past the compiler wouldn't stop you from being stupid. (I actually slightly misspoke--the *ptr is simply an error. That said, newer compilers do some rudimentary "sanity" checking of printf arguments. I've never seen how far the compiler goes, but have seen it complain on occasion when I was being dumb.)
modified on Thursday, July 30, 2009 2:35 PM
-
printf expects you to pass a value as an argument. By definition you can't resolve a void pointer. Be happy; in the past the compiler wouldn't stop you from being stupid. (I actually slightly misspoke--the *ptr is simply an error. That said, newer compilers do some rudimentary "sanity" checking of printf arguments. I've never seen how far the compiler goes, but have seen it complain on occasion when I was being dumb.)
modified on Thursday, July 30, 2009 2:35 PM
Joe Woodbury wrote:
printf expects you to pass a value as an argument. By definition you can't resolve a void pointer.
Agree with you..:thumbsup:
Joe Woodbury wrote:
Be happy;
Here too...:thumbsup: BUT,
Joe Woodbury wrote:
in the past the compiler wouldn't stop you from being stupid.
Is this solution to the problem I asked? :sigh:
-
Joe Woodbury wrote:
printf expects you to pass a value as an argument. By definition you can't resolve a void pointer.
Agree with you..:thumbsup:
Joe Woodbury wrote:
Be happy;
Here too...:thumbsup: BUT,
Joe Woodbury wrote:
in the past the compiler wouldn't stop you from being stupid.
Is this solution to the problem I asked? :sigh:
There is no solution except to case the pointer to something that resolves to an integer or long value (which is dangerous of the void* isn't actually pointing to an integer or long.) The point is that you could do
*(int*)ptr
. (I actually slightly misspoke--the *ptr is simply an error. That said, newer compilers do some rudimentary "sanity" checking of printf arguments. I've never seen how far the compiler goes, but have seen it complain on occasion when I was being dumb.) -
There is no solution except to case the pointer to something that resolves to an integer or long value (which is dangerous of the void* isn't actually pointing to an integer or long.) The point is that you could do
*(int*)ptr
. (I actually slightly misspoke--the *ptr is simply an error. That said, newer compilers do some rudimentary "sanity" checking of printf arguments. I've never seen how far the compiler goes, but have seen it complain on occasion when I was being dumb.)Joe Woodbury wrote:
The point is that you could do *(int*)ptr.
is working fine, But I want to print array so I tried following code which is not working, So can you please make it workable.
int arr[]={10,20,3,6};
void *ptr=(void *)arr;
int i=3;
while(i--)
{
printf(" %d",(*(int *)ptr));
(int *)ptr+=2;
} -
Joe Woodbury wrote:
The point is that you could do *(int*)ptr.
is working fine, But I want to print array so I tried following code which is not working, So can you please make it workable.
int arr[]={10,20,3,6};
void *ptr=(void *)arr;
int i=3;
while(i--)
{
printf(" %d",(*(int *)ptr));
(int *)ptr+=2;
}I don't understand why you are using a void*. It has no concept of size of what it's pointing to. You are removing type safety for no reason. That said, when you increment do arithmetic on a pointer, that arithmetic is done according to the size of the object pointed to by that pointer. In this case you are casting ptr to an (int*). Therefore, you only need to increment ptr by 1 to take you to the next position. (As an aside,
(int*)ptr += 1;
will not compile on many compilers--you would have to have ptr = (int*)ptr + 1; Two other points: You don't need to cast arr to a void*, that cast is automatic. You have an off by one error; you have four items in your array, but you only go through the loop three times. You simply shouldn't be using a void* in a case like this. It's an extremely bad idea. It would be better to use arr[] directly or if you must use a pointer, use an int*. -
I don't understand why you are using a void*. It has no concept of size of what it's pointing to. You are removing type safety for no reason. That said, when you increment do arithmetic on a pointer, that arithmetic is done according to the size of the object pointed to by that pointer. In this case you are casting ptr to an (int*). Therefore, you only need to increment ptr by 1 to take you to the next position. (As an aside,
(int*)ptr += 1;
will not compile on many compilers--you would have to have ptr = (int*)ptr + 1; Two other points: You don't need to cast arr to a void*, that cast is automatic. You have an off by one error; you have four items in your array, but you only go through the loop three times. You simply shouldn't be using a void* in a case like this. It's an extremely bad idea. It would be better to use arr[] directly or if you must use a pointer, use an int*.Joe Woodbury wrote:
I don't understand why you are using a void*
Just I wanted to check that is is possible to print array using void*, You helped me a lot and I Could, thanks. Last one Q.:
ptr = (int*)ptr + 1;
In this case,I don't understand why you incremented by 1, size of int is 2, ptr is void* but we are converting it into int so suppose to be incremented by 2; but it fails and how above code works fine ?
-
Joe Woodbury wrote:
I don't understand why you are using a void*
Just I wanted to check that is is possible to print array using void*, You helped me a lot and I Could, thanks. Last one Q.:
ptr = (int*)ptr + 1;
In this case,I don't understand why you incremented by 1, size of int is 2, ptr is void* but we are converting it into int so suppose to be incremented by 2; but it fails and how above code works fine ?
The size of an int varies by compiler and platform. On 16-bit operating systems, it is 16 bits or 2 bytes long. On 32-bit operating systems, it is 32 bits or 4 bytes long. Incrementing a pointer will cause it the memory reference to increase by the size of the object it points to. One way to look at pointers is to understand that
int* pArray
is functionally equivalent toint array[4]
. Likewise, assuming that pArray points to array, pArray[0] is the same as array[0]. Likewise pArray[1] is the same as array[1]. pArray[1] is the same as *(pArray + 1).