Skip to content

Instantly share code, notes, and snippets.

@SnowOnion
Created June 11, 2019 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SnowOnion/b103f2f2dddb64abf5de604773e49d7c to your computer and use it in GitHub Desktop.
Save SnowOnion/b103f2f2dddb64abf5de604773e49d7c to your computer and use it in GitHub Desktop.
See how http://pythontutor.com/c.html visualizes structures and pointers.
// for http://pythontutor.com/c.html#mode=edit
// forfor https://adnmb.com/t/18445046
#include<stdlib.h> // NULL
typedef struct account{
int num;
struct account *next;
} Account;
typedef Account* APtr;
APtr newA(int num_){
APtr n = (APtr)malloc(sizeof(Account));
n->num = num_;
n->next = NULL;
return n;
}
int main() {
// 新建两个结构体
APtr alice=newA(42);
APtr bob=newA(9);
// 两个结构体的 next 互相指向对方
alice->next = bob;
bob->next = alice;
// 利用临时变量 eve 来交换 alice 和 bob 两个指针所指的结构体
APtr eve = alice;
alice = bob;
bob = eve;
// 试一下 free 释放内存的可视化效果
free(bob);
free(alice);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment