#include<stdio.h>
#include<stdlib.h>
typedef struct ListNode{
int val;
struct ListNode *next;
}LNode,*LinkList;
//typedef struct ListNode LNode中LNode是结构体
// typedef struct ListNode *LinkList 应该将 typedef struct *一起看,LinkList 是结构体指针
void CreateList(LNode *header)
{
printf("&header=%#X header=%#X\n",&header,header);
header = (LNode*)malloc(sizeof(LNode)); //给header赋值了内存地址和大小
printf("&header=%#X header=%#X\n",&header,header);
header->val = 11;
header->next = NULL;
}
int main(void)
{
LinkList head = NULL; //head是结构体指针变量
printf("&head=%#X head=%#X\n",&head,head);
CreateList(head); //head是空的
if(head!=NULL)
{
printf("%d\n",head->val); //并没有输出
}
printf("&head=%#X head=%#X\n",&head,head);
free(head);
return 0;
}
实验结果
&head=0X60FEFC head=0
&header=0X60FEE0 header=0
&header=0X60FEE0 header=0X6D0D50
&head=0X60FEFC head=0
head的地址和它本身的值都没变化,说明函数不能改变head本身的值和对应的地址,只能改变head地址上存放的值。
#include<stdio.h>
#include<stdlib.h>
typedef struct ListNode{
int val;
struct ListNode *next;
}LNode,*LinkList;
void CreateList(LNode *header)
{
printf("%#X %#X\n",&header,header);
header->val = 11;
header->next = NULL;
}
int main(void)
{
LinkList head = (LinkList)malloc(sizeof(LNode));
printf("%#X %#X\n",&head,head);
CreateList(head);
if(head!=NULL)
{
printf("%d\n",head->val);
}
printf("%#X %#X\n",&head,head);
free(head);
return 0;
}
实验结果
0X60FEFC 0XA40D50
0X60FEE0 0XA40D50
11
0X60FEFC 0XA40D50