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

这组代码是打印出内容。

相关推荐
winfredzhang13 分钟前
使用Python和Selenium打造一个全网页截图工具
开发语言·python·selenium
mahuifa22 分钟前
(10)python开发经验
开发语言·python
_龙小鱼_31 分钟前
Kotlin扩展简化Android动画开发
android·开发语言·kotlin
小伍_Five36 分钟前
spark数据处理练习题详解【上】
java·开发语言·spark·scala
mascon1 小时前
C#自定义扩展方法 及 EventHandler<TEventArgs> 委托
开发语言·c#
Evand J1 小时前
【MATLAB例程】线性卡尔曼滤波的程序,三维状态量和观测量,较为简单,可用于理解多维KF,附代码下载链接
开发语言·matlab
苕皮蓝牙土豆2 小时前
C++ map容器: 插入操作
开发语言·c++
Dxy12393102162 小时前
Python 装饰器详解
开发语言·python
linab1122 小时前
mybatis中的resultMap的association及collectio的使用
java·开发语言·mybatis
NaclarbCSDN2 小时前
Java IO框架
开发语言·python