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;
}

首先是定义一个结构体数组student[3]

用一个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);

这组代码是打印出内容。

相关推荐
紫金修道4 小时前
【DeepAgent】概述
开发语言·数据库·python
Via_Neo4 小时前
JAVA中以2为底的对数表示方式
java·开发语言
书到用时方恨少!4 小时前
Python multiprocessing 使用指南:突破 GIL 束缚的并行计算利器
开发语言·python·并行·多进程
cch89185 小时前
PHP五大后台框架横向对比
开发语言·php
天真萌泪5 小时前
JS逆向自用
开发语言·javascript·ecmascript
野生技术架构师5 小时前
一线大厂Java面试八股文全栈通关手册(含源码级详解)
java·开发语言·面试
Q一件事6 小时前
R语言制图-相关性及关系网络图
开发语言·r语言
坊钰6 小时前
Java 死锁问题及其解决方案
java·开发语言·数据库
551只玄猫7 小时前
【数学建模 matlab 实验报告1】
开发语言·数学建模·matlab·课程设计·实验报告
三道渊8 小时前
C语言:文件I/O
c语言·开发语言·数据结构·c++