如何解决c - 如何将参数作为指向数组的指针传递
首先,我看过的现有解决方案都没有将参数作为数组,例如:
void inputCars(Car * cars[],int size);
这是我目前拥有的代码。
/******************************************************************************
WE WANT:
dynamic array of pointers to a
car structure
*******************************************************************************/
#include <string.h>
#include <stdio.h>
typedef struct {
char make[60];
double price;
int year;
}Car;
int size;
void inputCars(Car* cars[],int size);
int main(int argc,char** args)
{
printf("Enter how many Cars you wish to take inventory of: ");
scanf("%d",&size);
Car cars[size];
inputCars(&(cars),size);
}
void inputCars(Car * cars[],int size)
{
for(int i = 0; i < size; i++)
{
// TODO
}
}
当我尝试通过汽车阵列时,出现以下错误:
expected 'struct Car**' but argument is of type 'struct Car(*)[(sizetype)(size)]'
我明白 Car * cars[] 的要求,感谢您的帮助,但我对如何通过它感到困惑。
我不会大声改变函数中参数的方式,并且对如何传递数组感到非常困惑。
解决方法
在本声明中
Car cars[size];
声明了一个变长数组。
在这次通话中
inputCars(&(cars),size);
表达式 &cars
的类型为 Car( * )[size]
。但是对应的函数参数
void inputCars(Car* cars[],int size);
被声明为 Car * cars[]
,它被编译器调整为类型 Car **
并且没有从一种指针类型到另一种指针类型的隐式转换。所以编译器会发出错误信息。
在你的程序的注释中有写
WE WANT:
dynamic array of pointers to a
car structure
因此,您需要动态分配指向 Car
类型对象的指针数组,而不是声明可变长度数组。类似的东西
Car **cars = malloc( size * sizeof( Car * ) );
for ( int i = 0; i < size; i++ )
{
cars[i] = malloc( sizeof( Car ) );
}
在这种情况下,该函数可以像这样调用
inputCars( cars,size );
,
因为作为参数传递的数组衰减到指向该数组第一个元素的指针,所以这里有:
void inputCars(Car* cars[],int size);
有效:
void inputCars(Car** cars,int size);
所以你需要传递一个 Car**
类型的参数,而 Car[size]
不是。你取车地址(&car
)没有区别,参数还是不兼容。
您提供给函数的参数是一个指向 size
结构数组(长度为 Car
)的指针,而函数需要一个指向 Car
的指针:
'预期struct Car**
'。
如果你想传递一个指向给定长度的可变长度数组的指针,你需要将它的原型如下:
void inputCars(int size,Car (*cars)[size]);
或作为:
void inputCars(int size,Car (*cars)[*]);
稍后定义提供维度 ([*]
) 表达式:
void inputCars(int size,Car (*cars)[size]) { /*...*/ }
[*] 表达式可能更复杂,包括例如函数调用,并且它可能对定义完全私有,这就是为什么可以通过原型中的 *
符号省略它。
无论如何,如果维度表达式要基于尺寸参数,则尺寸参数需要在前。
示例程序:
#include <string.h>
#include <stdio.h>
typedef struct {
char make[60];
double price;
int year;
}Car;
void inputCars(int size,Car (*cars)[size]);
int main(int argc,char** args)
{
int size;
printf("Enter how many Cars you wish to take inventory of: ");
if(1!=scanf("%d",&size)) return 1;
Car cars[size];
inputCars(size,&(cars));
}
void inputCars(int size,Car (*cars)[size])
{
//prints the same number twice
printf("size=%d count=%zu\n",size,sizeof(*cars)/sizeof((*cars)[0]));
}
请记住,使用像这样不受限制的 VLA 不是很可取。您正在打开您的程序,面临堆栈溢出的风险(真实情况)。
,如果你不能改变原型,那么你需要分配指针数组,然后为汽车分配空间。
private DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("passwords").child(/*how so ever you want to nest data*/).setValue(password);
,
-
&(cars)
(或简称&cars
)是指向Car
的可变长度数组(VLA)的变量的地址 - {{1 }}。 - 简单地传递
Car (*)[size]
会使其衰减为cars
,而不是函数要求的Car*
(或Car*[]
)。
我怀疑您应该动态分配内存并将指针传递给拥有该内存的指针。
示例:
Car**
注意:这个答案引发了关于 #include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char make[60];
double price;
int year;
} Car;
void inputCars(Car *cars[],int size) {
// (*cars) below dereferences the Car** and gives a Car*
for(int i = 0; i < size; i++) {
// TODO
(*cars)[i].year = i; // just an example value to fill it with
}
}
int main() {
printf("Enter how many Cars you wish to take inventory of: ");
int size;
if(scanf("%d",&size) != 1 || size < 1) return 1;
Car* cars = malloc(size * sizeof(Car)); // or sizeof(*cars) - allocate
inputCars(&cars,size); // &cars is now a Car** (or Car*[])
free(cars); // release allocated memory
}
是否是 &cars
的讨论。如果它不是 Car**
,程序将有未定义的行为(非常糟糕) - 但我说 Car**
is 是 {{1 }} 并且该程序已经定义了行为,除非另有证明。
然而:即使这是正常工作的代码,它也可能不是您所期望的。如果这不能解决问题,请查看 Vlads 的答案。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。