c语言之指针指向结构体数组

在c语言中,指针指向结构体数组的方法是

在定义一个结构体数组之后,定义一个结构体指针

通过数组地址增减来控制在哪个数组元素

示例代码如下

cpp 复制代码
#include<stdio.h>
int main()
{
	struct test
	{
	char name[30];
	int age;
	char sex;
	int student_number;
	} student[3];
	struct test *p;

	for(p=student;p<student+3;p++)
	{
		printf("请输入姓名 年龄 性别 学号:");
		scanf("%s %d %c %d",p->name,&(p->age),&(p->sex),&(p->student_number));
	}
	for(p=student;p<student+3;p++)
		printf("姓名:%s\t 年龄:%d\t 性别:%c\t 学号:%d\n",p->name,p->age,p->sex,p->student_number);

	return 0;
}

首先是定义一个结构体数组student3

用一个for循环不断输入信息

for(p=student;p<student+3;p++)

{

printf("请输入姓名 年龄 性别 学号:");

scanf("%s %d %c %d",p->name,&(p->age),&(p->sex),&(p->student_number));

}

指针p指向数组student的第一个地址,然后p的值小于student数组的第四个地址时,p的值加1.在数组指针中,指针值+1是指向下一个数组元素的地址。

for(p=student;p<student+3;p++)

printf("姓名:%s\t 年龄:%d\t 性别:%c\t 学号:%d\n",p->name,p->age,p->sex,p->student_number);

这组代码是打印出内容。

相关推荐
小何code2 小时前
C语言【初阶】第1节,初识C语言
c语言·开发语言
代码小书生2 小时前
getpass,一个安全输入的 Python 库!
开发语言·python·安全
莫陌尛.2 小时前
Fuzzy C-Mean Clustering (FCM)
c语言·开发语言
YOU OU2 小时前
案例综合练习-博客系统
java·开发语言
其实防守也摸鱼2 小时前
告别单个变量,用列表和字典批量管理你的 Python 数据
开发语言·网络·软件测试·python·web安全·数据结构,编程教程
瑞雪兆丰年兮2 小时前
[从0开始学Java|第十八、十九天]API(常见API&对象克隆&正则表达式)
java·开发语言
KobeSacre2 小时前
JVM G1 垃圾回收器
java·开发语言·jvm
右耳朵猫AI2 小时前
JavaScript技术周刊 2026年第20周
开发语言·javascript·ecmascript
basketball6163 小时前
Go 语言从入门到进阶:5. 玩转Go函数
开发语言·后端·golang
多彩电脑3 小时前
Kivy如何自定义事件
开发语言·python