copying the content of a void-pointer to another void-pointer? 
Author Message
 copying the content of a void-pointer to another void-pointer?

Hi.

I would like to know how to copy the contents of a void-pointer to
another void-pointer. We tried something like:

for(i=0; i < 20; i++)
  *(ptr1+i) = *(ptr2+i);

(both pointer are allocated)

Then we learned that this isn't valid for void-pointers. How should
this be done then?

/Rick & Marie




Mon, 05 Nov 2001 03:00:00 GMT  
 copying the content of a void-pointer to another void-pointer?

Quote:

> I would like to know how to copy the contents of a void-pointer to
> another void-pointer. We tried something like:

> for(i=0; i < 20; i++)
>   *(ptr1+i) = *(ptr2+i);

> (both pointer are allocated)

> Then we learned that this isn't valid for void-pointers. How should
> this be done then?

Hi Richard,

why don't you use memcpy() ??
... memcpy(ptr1,ptr2,20); ... would do what you wanted to do in your example.

Regards Rainer



Mon, 05 Nov 2001 03:00:00 GMT  
 copying the content of a void-pointer to another void-pointer?
Hi Rickard, how are you?

Rickard ha scritto:

Quote:
> I would like to know how to copy the contents of a void-pointer to
> another void-pointer.

  What about memcpy(3) and related functions? ;)

        Best regards,
                Marco



Mon, 05 Nov 2001 03:00:00 GMT  
 copying the content of a void-pointer to another void-pointer?


Quote:
>I would like to know how to copy the contents of a void-pointer to
>another void-pointer. We tried something like:

>for(i=0; i < 20; i++)
>  *(ptr1+i) = *(ptr2+i);

>(both pointer are allocated)

Use memcpy, or cast the pointers to unsigned char *s and perform the
copy bytewise.

-- Mat.



Mon, 05 Nov 2001 03:00:00 GMT  
 copying the content of a void-pointer to another void-pointer?


Quote:
>Hi.

>I would like to know how to copy the contents of a void-pointer to
>another void-pointer. We tried something like:

>for(i=0; i < 20; i++)
>  *(ptr1+i) = *(ptr2+i);

>(both pointer are allocated)

>Then we learned that this isn't valid for void-pointers. How should
>this be done then?

The "contents" of a pointer is a pointer value. To copy that you simply
assign one pointer to another. To copy the data that the pointer points at
you can use memcpy() or copy the data byte by byte in a loop by
converting the pointers to pointers to unsigned char.

--
-----------------------------------------


-----------------------------------------



Mon, 05 Nov 2001 03:00:00 GMT  
 copying the content of a void-pointer to another void-pointer?

Quote:

> I would like to know how to copy the contents of a void-pointer to
> another void-pointer. We tried something like:

> for(i=0; i < 20; i++)
>   *(ptr1+i) = *(ptr2+i);

> (both pointer are allocated)

> Then we learned that this isn't valid for void-pointers. How should
> this be done then?

Rickard...

I'm guessing that you want to copy what one pointer points to to where
another points (I wrote that and even I don't like it! 8-)

Try casting the pointer to show what type of data you want to copy (It
/will/ make a difference to the compiler) - for example, if you're
copying longs (you're probably not, but the idea's the same):

for (i=0; i<20; i++) *((long *)ptr1 + i) = *((long *)ptr2 + i);

Morris Dovey
West Des Moines, Iowa USA



Mon, 05 Nov 2001 03:00:00 GMT  
 copying the content of a void-pointer to another void-pointer?

Quote:

>Hi.

>I would like to know how to copy the contents of a void-pointer to
>another void-pointer. We tried something like:

>for(i=0; i < 20; i++)
>  *(ptr1+i) = *(ptr2+i);

Thanks to recent advances in the C language you can now use the following
notation:

    ptr1[i] = ptr2[i];

Unfortunately, pointer arithmetic on, and dereferencing of, void * pointers
are invalid operations, regardless of how they are expressed. Assuming that you
want to copy individual bytes, you can convert the pointers to unsigned char *
and then do a byte by byte copy using the above loop:

        unsigned char *ucp1 = ptr1, *ucp2 = ptr2;

        /*...*/

        for (i = 0; i < 20; i++)
                ucp1[i] = ucp2[i];

There is a revolutionary new library function called memcpy() which
does the above using void * pointers:

        #include <stdlib.h>

        /*...*/

        memcpy(ptr1, ptr2, 20); /* WOW! */



Tue, 06 Nov 2001 03:00:00 GMT  
 copying the content of a void-pointer to another void-pointer?
For a single pointer simply use

*ptr1 = *ptr2;

for more than one you can use memcpy()

..-=ViKtOr=-..
------------------------------------------------------
Remove NOSPAM from the e-mail address before sending!!

Quote:


> >Hi.

> >I would like to know how to copy the contents of a void-pointer to
> >another void-pointer. We tried something like:

> >for(i=0; i < 20; i++)
> >  *(ptr1+i) = *(ptr2+i);

> Thanks to recent advances in the C language you can now use the following
> notation:

>     ptr1[i] = ptr2[i];

> Unfortunately, pointer arithmetic on, and dereferencing of, void * pointers
> are invalid operations, regardless of how they are expressed. Assuming that you
> want to copy individual bytes, you can convert the pointers to unsigned char *
> and then do a byte by byte copy using the above loop:

>         unsigned char *ucp1 = ptr1, *ucp2 = ptr2;

>         /*...*/

>         for (i = 0; i < 20; i++)
>                 ucp1[i] = ucp2[i];

> There is a revolutionary new library function called memcpy() which
> does the above using void * pointers:

>         #include <stdlib.h>

>         /*...*/

>         memcpy(ptr1, ptr2, 20); /* WOW! */



Wed, 07 Nov 2001 03:00:00 GMT  
 copying the content of a void-pointer to another void-pointer?


Quote:
> For a single pointer simply use

> *ptr1 = *ptr2;

<snip>

No, you can't do that. The OP's question (see thread title) referred
specifically to void pointers.

Try compiling your suggestion:

void CompTwoVoidPtrs(void *ptr1, void *ptr2)
{
  *ptr1 = *ptr2;

Quote:
}

and see how far you get.

--
Richard Heathfield

The bug stops here.



Thu, 08 Nov 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. void pointer (void *) : how to determine type?

2. from void (void*) to void (_cdecl*) (void*)

3. contents in a Void pointer

4. Casting function pointer to void pointer

5. problem with pointer to pointer to void

6. pointers to functions and pointers to void.

7. pointer arithmetic with a pointer to void

8. Can void-pointers be casted to function-pointers?

9. Pointer-arithmatic and void pointers

10. casting void pointer to be a pointer to a function

11. casting void pointer to be a pointer to a function

12. what's the difference between void main(void) and int main(void)

 

 
Powered by phpBB® Forum Software