如何将参数作为指向C

问题描述 投票:0回答:6
开始,我所看到的现有解决方案都没有参数作为数组,例如:

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[size];

c parameters arguments dynamic-arrays function-call
6个回答
3
投票
在此呼叫中

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**

1
投票
因此,您需要传递类型的参数,而不是。您采用汽车的地址没有区别,参数仍然不相容。

您作为函数的参数提供的内容是指向
Car[size]

结构的数组(长度

&car
)的指针,而该功能期望指向指向
size

的指针指针
'指望
Car
'
.


1
投票

如果您想将指针传递给给定长度的变异长度数组,则需要如下原型:

Car
或:
struct Car**

以后提供尺寸(void inputCars(int size, Car (*cars)[size]);

)表达式的定义:
void inputCars(int size, Car (*cars)[*]);


0
投票
[*]

表示法省略它的原因。

在任何情况下,如果要基于大小参数,则大小参数需要先出现。
示例程序:

void inputCars(int size, Car (*cars)[size]) { /*...*/ }

保留,使用这样的无限制VLA并不是很适合。您正在打开程序,冒着堆栈溢出的风险(真实的东西)。
    

如果您不能更改原型,那么您需要分配一系列指针,然后为汽车分配空间。
*


#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]));
}

(或简单地)是一个指向

-Varia -varia -variable长度阵列(vla)的变量的地址。 themply通过函数将其腐烂到

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: "); size_t size; if(scanf("%zu", &size) != 1 || size < 1) return 1; Car **cars = malloc(size*sizeof(*cars)); for(size_t n = 0; n < size; n++) { cars[n] = malloc(sizeof(*cars[0])); } inputCars(cars, size); //

,而不是函数所要求的

&(cars)
(或
&cars

)。


0
投票
示例:

Car
注:这个答案激发了关于

-1
投票
是否是
    cars
  • 的讨论。如果不是一个
    Car*
    ,则该程序将具有lunchende的行为
    (完全不好) - 但是我说
    Car*[]ISA
    Car**
    ,并且该程序已定义了行为,直到另有证明为止。
    如何:即使这是正确工作的代码,也可能不是您所期望的。查看vlads的答案,以获取替代方案。
        
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.