Thread: warning: passing argument 1 of ‘memcpy’ makes pointer from integer without a cast

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    warning: passing argument 1 of ‘memcpy’ makes pointer from integer without a cast

    Hey I'm getting this warning and when i run the program the warning actually leads to segementation fault.

    im trying to memcpy(unsigned int variable, (void*) buffer, 4);

    but I cant seem to get the variable to lose that warning sign

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    58
    Can you post the code including the headers

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You should be passing a pointer as the first parameter, a pointer to the destination you wish to fill.
    Code:
           #include <string.h>
    
           void *memcpy(void *dest, const void *src, size_t n);
    See the man page for details. What are you trying to do? Just copy variable into buffer? If so, you can just use an assignment . . . memcpy() is intended for when you wish to copy one array into another, or something similar.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    well im trying to copy a unsigned int with 4 bytes of the buffer so how do i get about doing that since the unsigned int is 4 bytes and the buffer has like 10 bytes how do i get 4 bytes from buffer into the int??
    unsigned char buffer[10]

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Just like dwks said - pass the address of the int, not the int itself.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Dino View Post
    Just like dwks said - pass the address of the int, not the int itself.
    Actually, that might not work as expected. Most machines are little endian, but there are some big endian machines. Meaning that a buffer \x01\x02\x03\x04 will usually become 0x04030201, but on other machines 0x01020304. Not to talk about the fact an integer is not even always 4 bytes.
    Strictly speaking, there is no way in doing this. Strictly speaking I don't believe you can even do anything completely portable in C. Imagine a 1 bit byte, 1 byte char, short, int and 1 byte of memory. Isn't that allowed according to the standards, as long as the character set only contains 2 characters..? Well, I'm not entirely sure, but I've never read a minimum width of a datatype, except int >= short >= char, char must be able to represent any character in the basic character set.
    Okay, let me switch off my pedantic mode for a bit.

    To properly do this, make sure that your data type is at least 4 bytes. I'd use uint32_t, even though it's not standards compliant (at least in C++, is it in C?) but even if it doesn't exist you should be able to add your own typedefs. Then, do something like this:
    Code:
    var = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
    Yes, I do know bit shifting to the left 0 positions doesn't have any effect, but I find the intentions more readable if we do it like this. It'll be optimized out anyway.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    uint32_t is part of the C99 standard. uint32_t(3) - Linux man page

    Strictly speaking I don't believe you can even do anything completely portable in C.
    Hmm, well, that's a pretty broad statement . . . .

    Anyway, this might be more complicated than it needs to be. Where do you get the data in buffer? If you're reading it from a file, for example, then you can use fread() to read an int directly. (Of course, that ignores the endianness issue, so maybe this way is better.)

    Another suggestion:
    Code:
    uint32_t i = 0;
    char buffer[] = {1, 2, 3, 4};
    int byte = 0;
    while(byte < sizeof(i)) {
        i |= buffer[byte] << ((sizeof(i) - byte - 1) * CHAR_BIT);
    }
    Never mind, that turned out to be way more complicated than it needed to be.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    okay thanks but I figured out that this could work but iono if theres a problem
    unsigned int variable
    memcpy((unsigned char*)& variable, (void*) buffer, 4);

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    memcpy(&variable, buffer, 4);
    No need for casts.
    Also, though not standard, uint32_t exists in boost, a widely popular C++ library, making it rather portable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 3
    Last Post: 01-14-2002, 12:13 PM