Thread: Copying elements between two void *

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    269

    Copying elements between two void *

    I have two void type arrays

    Code:
    void * shared;
    void * private;
    I want to copy elements from the private to the shared, but when I try, I get compile errors

    Code:
    int i;
    for(i = 0; i<size; i++)
    {
       shared[i] = private[i];
    }
    I get

    rvm.c:253: warning: dereferencing ‘void *’ pointer
    rvm.c:253: warning: dereferencing ‘void *’ pointer
    rvm.c:253: error: invalid use of void expression


    If I try

    Code:
    int i;
    for(i = 0; i<size; i++)
    {
       shared + i = private +i;
    }
    I get

    rvm.c:253: error: invalid lvalue in assignment


    it does not suffice to simply re-assign the pointers, as shared is allocated with mmap (with shared) and private is mmap (with private)

    Thank you so much!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    How about using memcpy? Something like: memcpy(shared, private, size) should do the trick.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by anduril462 View Post
    How about using memcpy? Something like: memcpy(shared, private, size) should do the trick.
    Brilliant. Thank you. It works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM
  2. What is this mean? static void *name (void *data)
    By beyonddc in forum C++ Programming
    Replies: 14
    Last Post: 05-05-2004, 03:06 PM
  3. Replies: 1
    Last Post: 02-02-2002, 12:25 PM
  4. int memCopy(void* , void*, int size)
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-19-2001, 03:02 PM