0

I'm trying out the memcpy.c implementation after understanding the code and the need for byte transfer or word transfer depending on the data received.

#include<stdio.h>

void* my_memcpy(void*,const void*,int); // return type void* - can return any type

struct s_{
        int a;
        int b;
};

int main(){
        struct s_ ss,dd;
        ss.a = 12;
        ss.b = 13;
        printf("\n sizeof(struct) : %d \n",sizeof(ss));
        my_memcpy(&dd,&ss,sizeof(ss));
        printf("\n a:%d b:%d \n",dd.a,dd.b);
        return 0;
}

void* my_memcpy(void* s,const void* d,int count){
        if(((s | d | count) & (sizeof(unsigned int)-1)0)){
                char* ps = (char* )s;
                char* pd = (char* )d;
                char* pe = (char* )s + count;
                while(ps != pe){
                        *(pd++) = *(ps++);
                }
        }
        else{
                unsigned int* ps = (unsigned int* )s;
                unsigned int* pd = (unsigned int* )d;
                unsigned int* pe = (unsigned int* )s + count;
                while(ps != pe){
                        *(pd++) = *(ps++);
                }
        }
}

Error : Invalid operands to binary | (void* and const void*).

I could not or a void* with const void*.

In the question I asked earlier in Understanding the implementation of memcpy() its typecasted to (ADDRESS).

What can be done to solve this error?

Community
  • 1
  • 1
Angus
  • 12,133
  • 29
  • 96
  • 151
  • 1
    The other question explained very clearly why the typecast is needed. Why did you take it out? Boolean operators can only take integer arguments, not pointers -- you need to cast the pointers to unsigned integers. – Barmar Oct 05 '13 at 11:32
  • @Barmar That should say "bitwise operators", right? – us2012 Oct 05 '13 at 11:47
  • The if statement is malformed. Typo? C also guarantees that unsigned ints will be at least two bytes large (16 bits). And count is used incorrectly in the second while loop. Needs to be count of integers in the structure, not sizeof structure in bytes. – willus Oct 05 '13 at 11:58
  • Also in the other question the third argument has the correct type, namely `sizeof`. Don't use `int` for sizes, it only will bring you trouble. – Jens Gustedt Oct 05 '13 at 21:15
  • Your question has nothing to do with your implementation of `memcpy`. You are well aware that the cast helped in the other question to do the bitwise operation and without it doesn't work on pointers. So why don't you ask directly about this, but send us through ready all of this? Then you might have found the other question and answer that Vadiklk linked to all by yourself. – Jens Gustedt Oct 05 '13 at 21:17
  • possible duplicate of [Why can't you do bitwise operations on pointer in C, and is there a way around this?](http://stackoverflow.com/questions/15868313/why-cant-you-do-bitwise-operations-on-pointer-in-c-and-is-there-a-way-around-t) – Jens Gustedt Oct 05 '13 at 21:19

2 Answers2

3

According to the standard, you cannot use bitwise operations on pointers: Why can't you do bitwise operations on pointer in C, and is there a way around this?

The easy solution (it's also pointed in the link), is to use casting.

Community
  • 1
  • 1
Vadiklk
  • 3,696
  • 5
  • 28
  • 44
-2
(s | d | count)

You should use logical or || rather than bitwise or |, because I suppose you are checking these parameters here, s and d should not be NULL, count should not be zero.

same as &, should be &&.

fluter
  • 13,238
  • 8
  • 62
  • 100
  • No, they are checking whether or not `s` and `d` are aligned at a `sizeof(unsigned int)` byte boundary. Using the logical operators would produce incorrect results here. – ApproachingDarknessFish Sep 25 '14 at 23:58