我正在偏移我的指針,如下面的代碼所示,以便複製到另一個結構。將指針移動到所需位置
#include <stdio.h>
struct a
{
int i;
};
struct test
{
struct a *p;
int x,y,z;
};
int main()
{
struct test *ptr = malloc(sizeof(struct test));
struct test *q = malloc(sizeof(struct test));
ptr->x = 10;
ptr->y = 20;
ptr->z = 30;
memcpy(&(q->x),&(ptr->x),sizeof(struct test)-sizeof(struct a*));
printf("%d %d %d\n",q->x,q->y,q->z);
return 0;
}
有沒有更好的方法來做我的memcpy()
? 我的問題是如果我認識到結構的成員,並且只想移動我的指針sizeof(struct a*)
並複製結構的其餘部分?
編輯:
我想複製結構的某些部分,但我不知道它的成員,但我知道我要跳過一些類型的變量作爲示例所示(結構A * )並複製結構的其餘部分。
的問題不是很清楚你是通過結構遍歷意味着複製? –
@GinuJacob是的..我的意思是我想複製一些基於 – Gopi
類型的結構部分不清楚的問題。你的意思是你想複製struct的所有成員,除了'p'? – Peter