Changing 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[] to the addRes() function, so I can change it in 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 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]'. 

When the program starts, it gets

 How many seats do you require? 

and does nothing after entering the value. Any help would be greatly appreciated!

+4
source share
2 answers

The declaration of int **seats[] in the function parameter is == int ***seats , and this means that the type *seats[i] is int* , and you assign it a number, that is, an incompatible error like:

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

Further in addRes(&seats);

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

Sentence:

As I can see in your code, you do not allocate memory for seats[i] in your addRes() function, and therefore, as I understand it, you do not need to declare the seat[] array as an array of pointers, but you need a simple int array.

Change declaration in main ():

 int *seats[50] = {0}; 

should be simple:

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

Next, simply pass seats[] the addRes() function array name, where the function declaration should be

 addRes(int* seats) or addRes(int seats[]) 

it makes your work pretty simple in the addRes() function, you can access its elements as seats[i] (and there is no need to use the additional * operator).

Array Length:

Another conceptual problem in the code that you use sizeof(*seats) to find out the length of the array. It is not right! because in addRes() the seats function is not larger than an array, but a pointer, so it will give you the size of the address (but not the length of the array).
And yes, to report the size of the seats[] function in addRes() , send an extra parameter called length, so finally declare addRes() as follows (read the 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 & 

Another problem that you currently do not encounter, but will soon encounter, is that you run the code that scanf () needs additional enter in the addRes() function. To solve this problem, change: scanf("%i\n", &s); like scanf("%i", &s); there is no need for an extra \n in the format string in scanf ().

+5
source
 int *seats[50] = {0}; 

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

Also your function signature for the array is incorrect, void addRes(int seats[]) will work fine.

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

 addRes(seats); 

Also, as indicated, when assigning an element to an array, you need to discard * :

 seats[i]=1; 

More than enough. The same goes for if and the like, where you do a comparison with an array element.

Regarding your addRes function:

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

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

You can fix it as follows:

  • Change the signature of the address function:

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

    • Directly: addRes(seats, 50);

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

Please note that the above only works in the local area of ​​arrays of this function, it will not work on the array that you received as an argument to the function (or dynamically allocated arrays).

Another problem is with scanf , you should give up \n . Use scanf("%i", &s);

+4
source

Source: https://habr.com/ru/post/1492563/


All Articles