69,371
社区成员
发帖
与我相关
我的任务
分享
#include <malloc.h>
#include <stdlib.h>
struct node
{
int key;
struct node* next;
};
struct node* test;
void change(struct node* k)
{
k->key=5;
k->next=NULL;
}
void main()
{
test = (struct node*)malloc(sizeof(test)); //使用之前要分配内存
test->key=1;
test->next=NULL;
printf("%d\n",test->key);
change(test);
printf("%d",test->key);
}
test =(struct node*) malloc(100 * sizeof(struct node));
struct node *p :指向struct node 类型的指针变量p
struct node p :数据类型是struct node 的变量p
此外,我提醒一点,struct是自定义类型,它和内嵌int float等类型一致,
只不过结构体支持标签
比如:
int a;
struct node{int data;} a;//不适用标签,直接用结构体定义变量
struct node{int data;};//先定义
struct node a;//使用标签定义变量
常见是使用typedef给个别名使用,这样可以缩短像这样的struct here_is_my_define_data_type;否则,每次使用都敲这么长,你会疯掉
[/quote]
那我这样描述对不对,test被传入子函数里面的时候传入的是指针值,只有对test->的元素赋值,才能改变子函数之外的值,否则在子函数里面,对传进来的(假设叫k)k,爱怎么折腾怎么折腾(比如k=NULL)也不会有影响