关于指针偏移

来源:互联网 发布:日本女生保养知乎 编辑:程序博客网 时间:2024/06/06 03:36
 

struct node
{
 char a;
 int b;
 int c;
};


set_node(struct node *node1, struct node *node2)
{
 char *p = (char *)node2;
 int *pint = NULL;
 *p = node2->a;
 printf("[%d]\n", *p);
 p+=4;    /*char 和下一个int要保持对齐,所以这里加4*/

 pint = (int *)p;
 *pint = node2->b;
 printf("[%d]\n", *pint);
 pint++;       /*这里指针已经转成int类型,只需要加 1*/
 *pint = node2->c;
 printf("[%d]\n", *pint);
}

int main()
{
  struct node node1;
 struct node node2;
 node2.a = 5;
 node2.b = 10;
 node2.c = 15;

    set_node(&node1, &node2);

 return 0;
}

原创粉丝点击