我想了解代碼和需要字節傳輸或字傳輸取決於接收到的數據後的memcpy.c實現。我的memcpy實現失敗
#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 *。
在我之前問過的Understanding the implementation of memcpy()的問題中,它被稱爲(ADDRESS)。
可以做些什麼來解決這個錯誤?
另一個問題非常清楚地解釋了爲什麼需要類型轉換。你爲什麼把它拿出來?布爾運算符只能使用整數參數,而不能使用指針 - 您需要將指針轉換爲無符號整數。 – Barmar
@Barmar應該說「按位運算符」,對不對? – us2012
if語句格式錯誤。錯字? C還保證無符號整數至少有兩個字節(16位)。並且在第二個while循環中count被錯誤地使用。需要在結構中進行整數計數,而不是以字節爲單位的sizeof結構。 – willus