c语言中->和.的区别

唯一的区别是->前面放的是指针,而.前面跟的是结构体变量。

如已定义了一个结构体struct student,里面有一个int a;

然后有一个结构体变量struct student stu及结构体变量指针struct student *p;

且有p=&stu,那么p->a和stu.a表示同一个意思:

#include<stdio.h>
#include<stdlib.h>

typedef struct 
{
	int ch;
}tree;

typedef struct 
{
	int ch;
}school;

void	main()
{
	tree* node1=malloc(sizeof(tree)) ;
	node1->ch=5;//这是在堆上开辟;
	
	school s;
	s.ch = 10;//这是在栈上开辟;

	printf("%d", node1->ch);
	printf("%d", node1.ch);//报错 node1是指针;
	printf("%d", s.ch);
	printf("%d", s->ch);//报错 s不是指针,s是变量名;

	system("pause");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值