Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying an array from another function in C

Here is my main function:

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;
    }
}

I am trying to pass seats[] into the addRes() function so I can modify it within addRes(). Here is the function:

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");
    }
}

It compiles with the warnings:

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]'.

On running the program it gets to

How many seats do you require?  

and does nothing after entering a value. Any help would be much appreciated!

like image 803
petehallw Avatar asked Jan 12 '23 21:01

petehallw


2 Answers

Declaration int **seats[] in function parameter is == int ***seats, and this means type of *seats[i] is int* and you are assigning a number to it, that is incompatible type error:

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

incompatible types 

Next in addRes(&seats);

seats in array of pointer its type if int*[50] that &seat is pointer of array and type of &seat is int*(*)[50] Where as function argument type is int ***, so again type incompatible error.
Notice you are also getting a reasonable error message from compiler: Expected 'int ***' but argument is of type 'int * (*)[50]'.

Suggestion:

As I can see in your code, you don't allocate memory for seats[i] in your function addRes() and So as I understand you not need to declare seat[] array as array of pointers but you need simple array of int.

Change declaration in main():

int *seats[50] = {0};

should be just:

int seats[50] = {0};
 // removed * before seats

Next just pass seats[] array's name to addRes() function where declaration of function should be

addRes(int* seats)

or  addRes(int seats[]) 

it make your work pretty simple in function addRes() you can access its elements as seats[i] ( and it no need to use extra * operator).

Length of array:

One more conceptional problem in your code that you are using sizeof(*seats) to know the length of array. Its wrong! because in addRes() function seats is not more an array but a pointer so it will give you the size of address ( but not array length).
And yes to inform about size of seats[] in addRes() function send an extra parameter called length, so finally declare addRes() as follows (read comments):

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

Call this function from main() as follows:

addRes(seats, 50);
  // no need to use &

One more problem that presently you are not facing but you will encounter soon as you will run you code that scanf() need extra enter in function addRes(). To resolve it change: scanf("%i\n", &s); as scanf("%i", &s); no need of extra \n in format string in scanf().

like image 123
Grijesh Chauhan Avatar answered Jan 17 '23 15:01

Grijesh Chauhan


int *seats[50] = {0};

This is an array of integer pointers, all you need is an actual array so drop the * resulting in int seats[50] = {0};.

Also your function signature for an array is wrong, void addRes(int seats[]) will do fine.

Finally, to pass an array to that new signature, you can pass the array directly without any unary address-of operators (arrays will decay to a pointer when passed as an argument to a function):

addRes(seats);

Also as pointed out, when assigning to an array element, you need to drop the *:

seats[i]=1;

Is more than enough. Same goes for the if statements and the like where you do a comparison against an array element.

Regarding your addRes function:

for(i=0;i<=sizeof(*seats);i++)

You will only get the size of the pointer this way, which on a 32bit machine is 4. This trick will not work on an array passed to a function. You will need to pass the array separately.

You can fix it in the following way:

  1. Change the function signature of address to this:

    • void addRes(int seats[], int size)
  2. Pass the size in one of the following ways in main:

    • Directly: addRes(seats, 50);

    • Indirectly: addRes(seats, sizeof(seats)/sizeof(int));

Note that the above only works on local to the scope of this function arrays, it won't work on an array you've obtained as an argument to a function (or dynamically allocated arrays).

Another issue is to do with scanf, you should drop the \n. Use scanf("%i", &s);

like image 35
Nobilis Avatar answered Jan 17 '23 15:01

Nobilis