当前位置: 首页 > news >正文

结构体指针和结构体数组指针

结构体指针和结构体数组指针是不同的类型。
结构体指针定义:Student *stu 结构体指针的步长是一个结构体的大小;
结构体数组指针定义:Student (*stu)[] 结构体数组指针的步长是整个结构体数组的大小。

例程:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _student{
char name[64];
int age;
} Student;

int myPrintf1(Student *stu,int * len) {//Student *stu:定义结构体数组指针
int lenth = *len;
Student *st = stu;
printf(“-----------------\n”);
printf(“%s\n”,func);
for(int i=0; i<lenth; i++) {
printf(“%s\n”,st[i].name);
printf(“%d\n”,st[i].age);
}

return 0;

}
int myPrintf2(Student (*stu)[],int * len) { //Student (*stu)[]:定义结构体数组指针stu
int lenth = *len;
printf(“-----------------\n”);
printf(“%s\n”,func);
for(int i=0; i<lenth; i++) {
printf(“%s\n”,(*stu)[i].name);
printf(“%d\n”,(*stu)[i].age);
}
return 0;
}
int myPrintf3(Student (*stu)[],int * len) { //Student (*stu)[]:定义结构体数组指针stu
int lenth = *len;
Student (*mystu)[];//定义结构体数组指针mystu
mystu = stu;//复制结构体数组指针
printf(“-----------------\n”);
printf(“%s\n”,func);
for(int i=0; i<lenth; i++) {

    printf("%s\n",(*mystu)[i].name);printf("%d\n",(*mystu)[i].age);
}
return 0;

}

int main() {
int len = 2;
Student stu[len];//定义结构体数组
strcpy(stu[0].name,“aaaaa”);
stu[0].age = 20;

strcpy(stu[1].name,"bbbb");
stu[1].age = 22;myPrintf1(stu,&len);//将结构体数组首元素地址(结构体指针)拷贝给形参myPrintf2(&stu,&len);//将结构体数组地址&stu拷贝给形参myPrintf3(&stu,&len);//将结构体数组地址&stu拷贝给形参return 0;

}

结果:

在这里插入图片描述

http://www.lryc.cn/news/139274.html

相关文章:

  • libdrm全解析二十 —— 源码全解析(17)
  • 基于docker搭建owncloud Harbor 构建镜像
  • 往Buildroot中增加Qt项目
  • C#-Tolewer和ToUpper的使用
  • RabbitMQ集群搭建和测试总结_亲测
  • SQLSTATE[IMSSP]: The active result for the query contains no fields.
  • 在Flutter应用内部实现分屏功能
  • Docker常用操作命令(二)
  • vue3 tailwindcss的使用
  • redis 基础篇(redis 理解)
  • C++系列-函数重载
  • leetcode-23.合并k个升序链表-day17
  • Linux scp命令
  • vue 简单实验 v-bind 变量与html属性绑定
  • 114.(cesium篇)cesium去掉时间轴并用按钮控制运动
  • 2023年清洁能源与智能电网国际会议(CCESG 2023)
  • RISC-V中国峰会 | 256核服务器高调亮相,谁与争锋?
  • 树套树小结
  • android 解决sdk代码冲突
  • C++逆天合集
  • stm32之15.超声波与灯光功能一起实现(进阶)
  • 美创科技荣获“2023年网络安全优秀创新成果大赛—杭州分站赛”两项优胜奖
  • 使用gdb+gdbserver远程调试aarch64平台程序
  • 【CesiumJS入门】(9)获取地表两点的距离及中心点——EllipsoidGeodesic
  • OLED透明屏介绍:领先科技的革命性创新
  • ESXI补丁更新
  • 【每日易题】数组下标的逆天用法——你见过把数组存储的值当作数组下标来解题的吗?
  • mysql基本操作
  • vue组件的使用
  • Axure设计之日期选择器(年月选择)