Created
June 11, 2019 01:06
-
-
Save SnowOnion/b103f2f2dddb64abf5de604773e49d7c to your computer and use it in GitHub Desktop.
See how http://pythontutor.com/c.html visualizes structures and pointers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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