c - 我的 memcpy 实现失败

标签 c

在了解代码以及根据接收到的数据进行字节传输或字传输的需要后,我正在尝试 memcpy.c 实现。

#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++);
                }
        }
}

错误:二进制操作数无效 | (void* 和 const void*)。

我不能或用 const void* 来实现 void*。

在我之前在 Understanding the implementation of memcpy() 中提出的问题中它被类型转换为(地址)。

如何解决此错误?

最佳答案

根据标准,不能对指针使用按位运算: Why can't you do bitwise operations on pointer in C, and is there a way around this?

简单的解决方案(也在链接中指出)是使用强制转换。

关于c - 我的 memcpy 实现失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19197272/

相关文章:

java - AttachCurrentThread 崩溃

c - 了解 file_operations 的 loff_t *offp

c - C中的位运算

c - 根据字节顺序对整数进行位操作

c - 汇编 如何将操作码 DIV 转换为 C 代码

c - pthread_create 并传递一个整数作为最后一个参数

c - 如何从 i2c io 扩展器 linux 请求 GPIO 中断(失败并显示 -EINVAL)

c - printf 如何影响 getrusage 在 C 中测量时间

c - 是>、<、!、&&、||的 "true"结果或 == 定义?

c++ - 使用多个类、额外的预先存在的 .a 库和其他依赖项制作库