Pointers/Memory Allocation   «Prev 

Casting the generic pointer type

Objective: Write and test a function that casts the generic pointer type to a standard working type.

Background

The problem with using void* is that it cannot be dereferenced. Thus, to perform useful work on a generic pointer, you must cast it to a standard working type, such as char*.

Instructions

Use the following code fragment to write and test the memcpy function so that the generic pointer type is cast to char*. This function copies the n characters from the variable based at s2 into the variable based at s1 and works with any two pointer types as actual arguments.

The code

void* memcpy(void* s1, const void* s2, unsigned n){
  char* from = s2, *to = s1;   //uses char type
  ....
}

Note that in addition to using void* as the generic pointer type, the function uses void* as the return type.
Paste the source code of the function below and click the

Submit

button when you are ready to submit this exercise.