以void * 存储的数据在复制时需注意的问题

来源:互联网 发布:乐高机器人编程软件 编辑:程序博客网 时间:2024/04/29 17:13

 

 

 

typedef struct _node  

{

void *data;

struct _node *next;

/* struct _node *pre;*/

}NODE;

 

typedef struct 

{

NODE *head;

NODE *last;

int size;

}LIST;

 

 

 

int getNumFromList(LIST *l)

{

NODE *n = l->head;

int  tens = 1, num = 0, i;

int temp;

for(i=0; i<l->size-1; i++)

{

tens *= 10;

}

while(n)

{

temp = (int *)(n->data);

temp *= tens;

tens /= 10;

num += temp;

n = n->next;

}

 

return num;

}

 

 

当temp为int时 temp = (int *)(n->data);或temp = (int )(n->data);都是将n->data的内存地址付给temp

所以 复制数据时应用相应的指针变量

int *temp;      

temp = (int *)(n->data);或temp = (int )(n->data);

原创粉丝点击