我有一个C程序,我必须对其进行修改,以便a链接至其自身,b链接至cc链接至b

它确实可以编译。但是我不确定我是否做对了。

#include <stdio.h>

int main(){
  struct list {
    int data;
    struct list *n;
  } a,b,c;

  a.data=1;
  b.data=2;
  c.data=3;
  b.n=c.n=NULL;
  a.n=a.n=NULL;
  a.n= &c;
  c.n= &b;

  printf(" %p\n", &(c.data));
  printf("%p\n",&(c.n));
  printf("%d\n",(*(c.n)).data);
  printf("%d\n", b.data);
  printf("integer %d is stored at memory address %p \n",a.data,&(a.data) );
  printf("the structure a is stored at memory address %p \n",&a );
  printf("pointer %p is stored at memory address %p \n",a.n,&(a.n) );
  printf("integer %i is stored at memory address %p \n",c.data,&(c.data) );
  getchar();
  return 0;
}


我如何有指向自身的指针链接?

最佳答案

鉴于:

list *a, *b, *c;
a=(list *)malloc(sizeof (list));
b=(list *)malloc(sizeof (list));
c=(list *)malloc(sizeof (list));



本身的链接

a->n=a;

b链接到c

b->n=c;

c链接到b

c->n=b;



你能看到你做错了什么吗?

关于c - 如何使C程序指针指向自身,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5173286/

10-09 15:51