2

I want to copy the bits from one void * to another void *.

How can I do it?

I tried this:

static void* copyBlock(void* ptr) {
    if (!ptr) {
        return NULL;
    }
    int sizeOfBlock=*(int*)ptr+13;
    void* copy = malloc(sizeOfBlock);
    if (!copy) {
        return NULL;
    }
    for(int i=0;i<sizeOfBlock;i++){
        *(copy+i)=*(ptr+i);
    }
    return copy;
}

but I get: invalid use of void expression

1
  • Just use bog-standard memcpy(3) Commented Dec 19, 2015 at 22:00

3 Answers 3

4

You cannot dereference, perform pointer arithmetic, indexing a void pointer because it has no base type or object size. You must therefore cast the void pointer to the a pointer to the type of the data units you are copying so that the compiler will know the size of the data to copy.

All that said, you'd be better off using:

memcpy( copy, prt, sizeOfBlock ) ;
Sign up to request clarification or add additional context in comments.

Comments

2

This design (storing block size inside of a block without any struct) seems dangerous to me, but I still know the answer.

*(copy+i)=*(ptr+i);

Here you get the error, because you can't dereference a void pointer. You need to cast it to pointer to something before. Like this:

((char *)copy)[i] = ((char *)ptr)[i];

2 Comments

but I do not know which data stored in the block. It may be int for example. So how can I dereference it to char *?
@OhadShamir You can always cast any data pointer to char pointer and dereference it. Think about it, char is always a single byte. You can't assign mysterious void in C, you can assign bytes and other types.
0

You should use the memcpy function:

memcpy(copy, ptr, sizeOfBlock);

Depending on the compiler settings (you may be compiling as C++ and not as C), you may need to cast the pointers to a char pointer:

memcpy((char *) copy, (const char *) ptr, sizeOfBlock);

Note: The parameter of the function should be const char *ptr, to make sure you don't change the contents of ptr by mistake.

7 Comments

No conforming C compiler will require casts on the arguments to memcpy. (C++ does, but the question is tagged C.)
True. I added that comment in case he was talking about C in a C++ environment. You never know...
What do you mean by "C in a C++ environment"? If you're talking about C++ compatibility (and IMHO there's no real reason to), you should mention that in your answer. But there's no need to cater to C++ restrictions when writing C code, except in the rare cases where you need your code to compile both as C and as C++.
"C++ environment" means any situation where the compiler is compiling with C++ compatibility. A person can start up an IDE without understanding the settings or run a command-line compiler with the C++ option set. As this should be C, I gave the first version. If the compiler complains, then he may be using C++. Instead of trying to figure out why it is set to C++, I mentioned the second option to save time and frustration.
The solution is not to hack the code so it can be compiled as if it were written in a different language. The solution is to figure out how to use the C compiler correctly. Either way, if your concern is C++ compatibility, you really should mention that in your answer; otherwise your "Depending on the compiler settings" remark is just confusing. (If you make that change, flag me in a comment.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.