Thread: problems using memcpy

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    problems using memcpy

    hey, i have a problem trying to figure out what i'm doing wrong with memcpy,

    i get the following to errrors when i try to run this code.

    a4.c:222: error: incompatible type for argument 1 of `memcpy'
    a4.c:222: error: incompatible type for argument 2 of `memcpy'

    memArray is an array of memRec structures.


    Code:
    for(i=foundIndex; i<memLoc; i++) {
     memcpy(memArray[i],memArray[i+1],sizeof(memRec));
     }
    Could someone please tell me why my first and second arguments are incompatible types for memcpy and what they should be instead.

    Thanx a heap

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by newby01 View Post
    hey, i have a problem trying to figure out what i'm doing wrong with memcpy,

    i get the following to errrors when i try to run this code.

    a4.c:222: error: incompatible type for argument 1 of `memcpy'
    a4.c:222: error: incompatible type for argument 2 of `memcpy'

    memArray is an array of memRec structures.


    Code:
    for(i=foundIndex; i<memLoc; i++) {
     memcpy(memArray[i],memArray[i+1],sizeof(memRec));
     }
    Could someone please tell me why my first and second arguments are incompatible types for memcpy and what they should be instead.

    Thanx a heap
    You need the address of the memArray[i], not the memArray[i] itself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Assuming you have an array like this:
    Code:
    char str[100];
    str[1] is just a single char, not a char*

    (str + 1) is a char* because it takes the address of the first element of str and adds 1 to the pointer address, creating a new address.

    &str[1] is the address of the 2nd char in the array, so it's also a char*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  2. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 11:35 AM
  3. MD5 hashing problems =(
    By Uncle Rico in forum C Programming
    Replies: 4
    Last Post: 02-28-2005, 10:31 AM
  4. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 06:25 AM
  5. memcpy
    By doubleanti in forum C++ Programming
    Replies: 10
    Last Post: 02-28-2002, 04:44 PM