从C中的另一个函数修改数组

pet*_*llw 4 c arrays pointers function

这是我的main功能:

main(){

    int *seats[50] = {0};
    char x;

    do{
        printf("A-Add Reservation\tC-Cancel Reservation\n");
        scanf("%c", &x); 
    } while(x != 'a' && x != 'c');

    switch(x){
    case 'a':
        addRes(&seats); 
        break;
    default: 
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图传递seats[]addRes()函数,所以我可以在其中修改它addRes().这是功能:

void addRes(int **seats[]){
    int s, i, scount=0, j=0, k=0, yourseats[]={0};
    printf("How many seats do you require? ");
    scanf("%i\n", &s);
    for(i=0;i<=sizeof(*seats);i++){
        if(*seats[i] == 0)
            scount++;
    }
    if(scount >= s){
        for(i=0;i<=s;){
            if(*seats[i] == 0){
                yourseats[j]=i;
                *seats[i]=1;                
                i++; j++;
            }
            else i++;
        }
        printf("Your seat numbers are: \n");
        while(k < j){
            printf("%i\n", yourseats[k]); 
            k++;
        }   
    }
    else {
        printf("Sorry, there are not enough seats available.\n");
    }
}
Run Code Online (Sandbox Code Playgroud)

它汇编了警告:

Line 15 (*seats[i]=1;) Assignment makes pointer from integer without a cast.  
Line 53: (addRes(&seats);) Passing argument 1 of 'addRes' from incompatible pointer       type.
Line 3: (void addRes(int ** seats[]){) Expected 'int ***' but argument is of type  'int *(*)[50]'.
Run Code Online (Sandbox Code Playgroud)

在运行程序时它会到达

How many seats do you require?  
Run Code Online (Sandbox Code Playgroud)

输入值后不执行任何操作.任何帮助将非常感激!

Gri*_*han 5

int **seats[]函数参数中的声明是== int ***seats,这意味着类型*seats[i]是,int*并且您正在为其分配一个数字,即不兼容的类型错误:

*seats[i] = 1; 
  ^         ^ int  
  |        
    int* 

incompatible types 
Run Code Online (Sandbox Code Playgroud)

接下来 addRes(&seats);

seats指针的数组的类型,如果int*[50]&seat数组的指针和类型&seatint*(*)[50]凡为函数参数的类型是int ***,所以再次键入不相容错误.
请注意,您还从编译器收到了一条合理的错误消息:Expected 'int ***' but argument is of type 'int * (*)[50]'.

建议:

正如我在你的代码中看到的,你没有为seats[i]你的函数分配内存addRes(),所以我理解你不需要将seat[]数组声明为指针数组,但是你需要简单的int数组.

main()中的变更声明:

int *seats[50] = {0};
Run Code Online (Sandbox Code Playgroud)

应该只是:

int seats[50] = {0};
 // removed * before seats
Run Code Online (Sandbox Code Playgroud)

接下来只是将seats[] 数组的名称传递给addRes()函数声明的函数

addRes(int* seats)

or  addRes(int seats[]) 
Run Code Online (Sandbox Code Playgroud)

它使你的工作在功能上非常简单,addRes()你可以访问它的元素seats[i](并且它不需要使用额外的*运算符).

数组长度:

代码中的另一个概念问题,您正在使用它sizeof(*seats)来了解数组的长度.这是不对的!因为在addRes()函数seats中不是一个数组而是一个指针,所以它会给你地址的大小(但不是数组长度).
是的,告知的尺寸seats[]addRes()函数发送称为长一个额外的参数,所以最后声明addRes()如下(阅读评论):

void addRes(int seats[], int length){
     // access seat as 
     //  seat[i] = 10;
     // where i < length 
}
Run Code Online (Sandbox Code Playgroud)

从main()调用此函数,如下所示:

addRes(seats, 50);
  // no need to use &
Run Code Online (Sandbox Code Playgroud)

还有一个问题,你现在还没有面对,但你会很快遇到,因为你将运行scanf()需要额外enter功能的代码addRes().解决它的变化:scanf("%i\n", &s);因为scanf()中的格式字符串scanf("%i", &s);不需要额外的\n.