在了解代码以及根据接收到的数据进行字节传输或字传输的需要后,我正在尝试 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/