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
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
Can you post the code including the headers
You should be passing a pointer as the first parameter, a pointer to the destination you wish to fill.
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.Code:#include <string.h> void *memcpy(void *dest, const void *src, size_t n);
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.
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]
Just like dwks said - pass the address of the int, not the int itself.
Mainframe assembler programmer by trade. C coder when I can.
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:
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.Code:var = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
uint32_t is part of the C99 standard. uint32_t(3) - Linux man page
Hmm, well, that's a pretty broad statement . . . .Strictly speaking I don't believe you can even do anything completely portable in C.
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:
Never mind, that turned out to be way more complicated than it needed to be.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); }
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.
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);