关于网友提出的“ 形参是结构体数组,请问 我如何再函数体内得到这个形参的长度呢??”问题疑问,本网通过在网上对“ 形参是结构体数组,请问 我如何再函数体内得到这个形参的长度呢??”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 形参是结构体数组,请问 我如何再函数体内得到这个形参的长度呢??
描述: A.c
//结构体
struct Student {
int num;
char name[20;
};
//定义
struct Student student[100];
//调用函数,参数为数组结构体
testStudentInfostudent(student);
----------------------
B.c
int testStudentInfo(struct Student *stu){
//在此我如何获取到stu的长度??????????
int i=0;
for(i=0;i<>
(stu)[i].age = 10;
}
return 1;
}
解决方案1: 数组退化为指针了,这种情况下是拿不到长度的,
要想拿到可以通过函数模板的方式拿到
解决方案2: 传入的只是数组首地址,因为数组的长度在内存中并没有事先存放所以无从获取,只能手动传递
解决方案3: 加一个入参arraylen,表示数组长度
int testStudentInfo(struct Student *stu, int arraylen)
解决方案4: int testStudentInfo(struct Student *stu)传入得是指针,不能得到长度,需要加上一个int _length长度也传入函数内。
int testStudentInfo(struct Student *stu,int _length)
以上介绍了“ 形参是结构体数组,请问 我如何再函数体内得到这个形参的长度呢??”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2582394.html