Modifying an array from another function in C - 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!

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().

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:
Change the function signature of address to this:
void addRes(int seats[], int size)
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);

Related

how to pass a condition as parameter to function in C?

I have created an array of function pointers to swap two variables.
pointer pointing to these functions namely: swap1, swap2. swap3 and swap4.
swap2 is swaping using pointer passed as arguments.
but while declaring the function pointer, only int and int are passed as arguments. after compiling this causes many warnings.
so do we have a better way of passing the argument, where we put condition in function call itself.
code is given below.
#include <stdio.h>
int swap1(int ,int );
int swap2(int* ,int* );
int swap3(int ,int );
int swap4(int, int);
int swap1(int a,int b)
{
int temp=a;
a=b;
b=temp;
printf("swapped with 3rd variable :%d, %d\n", a,b);
}
int swap2(int *a,int *b)
{
int temp = *a;
*a = *b;
*b = temp;
printf("swapped with pointer :%d, %d\n", *a,*b);
}
int swap3(int a,int b)
{
a+=b;
b=a-b;
a-=b;
printf("swapped with 2 variable :%d, %d\n", a,b);
}
int swap4(int a,int b)
{
a=a^b;
b=a^b;
a=a^b;
printf("swapped with bitwise operation :%d, %d\n", a,b);
}
int main()
{
int ch;
int a=3;
int b=4;
printf("enter the option from 0 to 3\n");
scanf("%d",&ch);
int (*swap[4])(int, int) ={swap1,swap2,swap3,swap4};// function pointer
/*can we pass something like int(*swap[4]( condition statement for 'pointer to variable' or 'variable')*/
if (ch==1)// at '1' location, swap2 is called.
{
(*swap[ch])(&a,&b);//passing the addresses
}
else
{
(*swap[ch])(a,b);
}
return 0;
}
some warnings are as follows.
at line 36 in file '9e748221\script.c'
WARNING: found pointer to int where int is expected
at line 47 in file '9e748221\script.c'
WARNING: found pointer to int where int is expected
at line 47 in file '9e748221\script.c'
Well yes. There are a number of problems with your code, but I'll focus on the ones to which the warnings you presented pertain. You declare swap as an array of four pointers to functions that accept two int arguments and return an int:
int (*swap[4])(int, int)
Your function swap2() is not such a function, so a pointer to it is not of the correct type to be a member of the array. Your compiler might do you a better favor by rejecting the code altogether instead of merely emitting warnings.
Having entered a pointer to swap2() into the array anyway, over the compiler's warnings, how do you suppose the program could call that function correctly via the pointer? The type of the pointer requires function arguments to be ints; your compiler again performs the dubious service of accepting your code with only warnings instead of rejecting it.
Since the arguments in fact provided are the correct type, it might actually work on systems and under conditions where the representations of int and int * are compatible. That is no excuse, however, for writing such code.
Because pointers and ints are unchanged by the default argument promotions, one alternative would be to omit the prototype from your array declaration:
int (*swap[4])() = {swap1,swap2,swap3,swap4};
That says that each pointer points to a function that returns int and accepts a fixed but unspecified number of arguments of unspecified types. At the point of the call, the actual arguments will be subject to the default argument promotions, but that is not a problem in this case. This option does prevent the compiler from performing type checking on the arguments, but in fact you cannot do this correctly otherwise.
Your compiler might still warn about this, or could be induced to warn about it with the right options, but the resulting code nevertheless conforms and does the right thing, in the sense that it calls the pointed-to functions with the correct arguments.
To deal with the warnings first: You declare an array of functions which take int parameters. This means that swap2 is incompatible with the type of element for the array you put it in. This will generate a diagnostic.
Furthermore, when you call one of the functions in the array, the same array declaration tells the compiler that the parameters need to be ints not pointers to int. You get two diagnostics here, one for each parameter.
To fix the above all your functions need to have compatible prototypes with the element type of the array. Should it be int or int*? This brings us to the other problem.
C function arguments are always pass by value. This means that the argument is copied from the variable onto the stack (or into the argument register depending on the calling convention and argument count - for the rest of this post, I'll assume arguments are placed on the stack for simplicity's sake). If it's a literal, the literal value is put on the stack. If the values on the stack are changed by the callee no attempt is made by the caller, after the function returns, to put the new values back in the variables. The arguments are simply thrown away.
Therefore, in C, if you want to do the equivalent of call by reference, you need to pass pointers to the variables you use as arguments as per swap2. All your functions and the array should therefore use int*. Obviously, that makes one of swap1 and swap2 redundant.
The correct array definition is
int (*swap[4])(int*, int*) = {swap1, swap2, swap3, swap4};
and the definition of each function should be modified to take int* parameters. I'd resist the temptation to use int (*swap[4])() simply because it circumvents type safety. You could easily forget the & in front of an int argument when the called function is expecting a pointer which could be disastrous - the best case scenario when you do that is a seg fault.
The others have done great work explaining what the problems are. You should definitely read them first.
I wanted to actually show you a working solution for that sort of problem.
Consider the following (working) simple program :
// main.c
#include <stdio.h>
void swap1(int* aPtr, int* bPtr) {
printf("swap1 has been called.\n");
int tmp = *aPtr;
*aPtr = *bPtr;
*bPtr = tmp;
}
void swap2(int* aPtr, int* bPtr) {
printf("swap2 has been called.\n");
*aPtr += *bPtr;
*bPtr = *aPtr - *bPtr;
*aPtr -= *bPtr;
}
int main() {
int a = 1, b = 2;
printf("a is now %d, and b is %d\n\n", a, b);
// Declare and set the function table
void (*swapTbl[2])(int*, int*) = {&swap1, &swap2};
// Ask for a choice
int choice;
printf("Which swap algorithm to use? (specify '1' or '2')\n>>> ");
scanf("%d", &choice);
printf("\n");
// Swap a and b using the right function
swapTbl[choice - 1](&a, &b);
// Print the values of a and b
printf("a is now %d, and b is %d\n\n", a, b);
return 0;
}
First of, if we try to compile and execute it:
$ gcc main.c && ./a.out
a is now 1, and b is 2
Which swap algorithm to use? (specify '1' or '2')
>>> 2
swap2 has been called.
a is now 2, and b is 1
As myself and others mentioned in answers and in the comments, your functions should all have the same prototype. That means, they must take the same arguments and return the same type. I assumed you actually wanted to make a and b change, so I opted for int*, int* arguments. See #JeremyP 's answer for an explanation of why.

Warning passing argument 2 of 'finder' makes pointer from integer without a cast

I am trying to pass a user entered variable "find" into this function and return the subscript location of the number (in an existing array) that the user entered. I saw some other posts about this but couldn't understand really what was being explained. Sorry, beginner student.
It isn't quite complete but I cannot compile due to a couple of errors that I am not sure about.
Warning passing argument 2 of 'finder' makes pointer from integer without a cast. Its pointing at:
num_loc = finder(find, sort_num[10]);
Here I am setting "num_loc" to the return of "where" in the function
num_loc = finder(find, sort_num[10]);
printf( "\nYour number is located in memory location %d of the array",num_loc );
"[Note] expected 'int *' but argument is of type 'int'" which is pointing to my function prototype.
//fprototype outside the main at the beginning of the file
int finder(int f,int x[]);
Here is my function:
//function located at the end of the file outside the main
int finder(int f, int x[])
{
int found = 0;
int where;
int i = 0;
while (found != 1){
if (x[i] == f){
found = 1;
where = i;
return where;
}
else{
++i;
}
}
}
num_loc = finder(find, sort_num[10]);
is equivalent to
int num = sort_num[10]; // Problem. Accessing array out of bounds.
num_loc = finder(find, num); // Problem. Using an `int` when an `int*` is expected.
// That's what the compiler is complaining about.
You need to use just sort_num in the call to finder.
num_loc = finder(find, sort_num);
The real solution involves changing finder to accept another argument that indicates the number of elements in sort_num. Otherwise, you run the risk of accessing the array out of bounds. It can also be simplified quite a bit.
int finder(int f, int x[], int arraySize)
{
for ( int i = 0; i < arraySize; ++i )
{
if (x[i] == f)
{
return i;
}
}
// Not found
return -1;
}
and then call it with:
num_loc = finder(find, sort_num, 10);
This is the first part of your function definition:
int finder(int f, int x[])
Your second argument is an int pointer, which the compiler is telling you by saying :
expected 'int *'
You called your function with this:
num_loc = finder(find, sort_num[10]);
If sort_num is an integer array, then sort_num[10] evaluates to the integer at the 11th place in that array. So you're passing your finder function that integer, instead of an int pointer. If sort_num is an integer array, re-write your call as:
num_loc = finder(find, sort_num);
This way you will be passing an int pointer that holds the address of the first element in the sort_num array.

Array not being passed to Function Method

I'm currently trying to pass an array of (values[3]) which it's first 3 values contain user input. However, I'm getting the error "expected int* but argument is type of int". I've tried to pass to method1, without the iteration of 'i', using the first three positions in the values array, but that's as far as I managed to attempt to fix it, any help would be much appreciated!
int main(void)
{
int i;
int values[3];
printf("Enter three consecutive numbers (With spaces between)");
scanf("%d %d %d",&values[0],&values[1],&values[2]);
for(i=0;i<3;i++)
method1(values[i]);
}
int method1(int values[3])
{
}
You cannot pass an array to a function as an array - it "decays" to a pointer. There are two issues with your code:
There is no forward declaration of method1 visible at the point of invocation, and
You are passing values[i], a scalar value in place of an array.
A forward declaration is necessary because otherwise the compiler would assume that method1 takes an returns an int, which is not true. Add this line before main
int method1(int values[]);
You could also move method1 above main to fix this without providing a forward declaration. Also, 3 inside square brackets is not necessary, because the array is passed like a pointer anyway.
If you want to pass the entire array, pass values. Of course, i becomes unnecessary:
int res = method1(values);
#include <stdio.h>
int main(void)
{
int i;
int values[3];
printf("Enter three consecutive numbers (With spaces between)");
scanf("%d %d %d",&values[0],&values[1],&values[2]);
method1(values, 3);
getchar();
getchar();
return 0;
}
int method1(int* values, int size)
{
int i;
for(i=0; i<size; i++){
printf("%d ", values[i]);
}
return 1;
}
In C, arrays are passed by reference, you can see method1's first argument is int* that refers first element of array, and second argument is size of this array.

I have a issue with passing by reference using string

This is the question i am working on.
"A simple encryption scheme named "rotate13" for encrypting text is to convert each letter (a…z or A...Z) to another letter by counting forward 13 letters, looping around from 'z' to 'a' or 'Z' back to 'A' if necessary.
Write a function named rotate13 which takes a single null-terminated string as a parameter
and converts it to its rotate13 equivalent. The function should modify the string directly, and it
should not return anything. Remember, only letters should change; all other characters remain
the same. You may assume that ctype.h is correctly included at the top of your program so
that you can use any functions within the library if you wish. "
And this is the error i keep getting
"error C2664: 'rotate13' : cannot convert parameter 1 from 'char (*)[10]' to 'char *[]'"
Thanks for the help. It will help me in my revisions for finals.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int rotate13(char *array[]);
int size=10;
int main()
{
char arr[10];
printf("Please enter the letters you wish to encrypt: ");
scanf("%s",&arr);
printf("%s",arr);
rotate13(&arr);
system ("pause");
return 0;
}
int rotate13(char *array[])
{
int pointer;
while(*array[pointer]!='\0')
{
if(isupper(*array[pointer])!=0)
{
if(*array[pointer]<'N')
*array[pointer]=*array[pointer]+13;
else
*array[pointer]=*array[pointer]-13;
}
else
{
if(*array[pointer]<'n')
*array[pointer]=*array[pointer]+13;
else
*array[pointer]=*array[pointer]-13;
}
pointer++;
}
printf("%s", *array);
return 0;
}
Remove & from scanf("%s",&arr);
scanf("%s",arr);
rotate13 is expecting an argument of type char ** but, by passing &arr you are passing it an argument of type int (*)[10]. Passing arr to rotate13 will solve your problem.
rotate13(arr);
You want to pass a string as the parameter of rotate13, so use either
int rotate13(char *array);
or
int rotate13(char array[]);
and pass arr like rotate13(arr). the same for scanf("%s",arr);
And inside the function rotate13, pointer is an int(bad variable name) that isn't initialized. To access the characters, use array[pointer], not *arr[pointer].
The function should look like this:
int rotate13(char array[])
{
int n;
while(array[n]!='\0')
{
Use rotate13(arr); instead of rotate13(&arr); and parameter should be char [] instead of char *[]
rotate13(&arr); sends the address of the array to the function which causes the parameter mismatch
As other answers have said, you're better of with int rotate13(char array[]). To understand the context, you should read this wonderful page: http://c-faq.com/aryptr/
Basically, passing a pointer to an array is redundant here, because in C when you pass an array to a function, what's actually passed is a pointer to its first element. Arrays are inherently passed by reference.
Some things need to be explained. "string" in C is the address of a character buffer.
Since the identifier arr in char arr[10]; degrades to a pointer to the first element of the array, So you don't need to specify a pointer (i.e &) to the string in the argument to scanf.
By passing &arr in your scanf as scanf("%s",&arr); the pointer passed to scanf is now a doubly-indirect pointer (it is a pointer to a pointer to the beginning of the buffer) and will likely cause the program to crash or other bad behaviour.
The strings in c, dont require &array as they implicitly pass address of the first element of the character array
So your two statments scanf("%s",&arr) should be simply scanf("%s",arr) and rotate13(&arr) should be rotate13(arr). notice that address of the first element are implicitly passed in the function calls.
Your function rotate13(char *array[ ]) is completely wrong way of doing it
char arr[10] -> can hold single string ( simply an array )
char *array[] -> can hold multiple strings ( also called array of pointers)
the formal parameter should be rotate13(char array[]) or rotate13(char *array).
After seeing your code I believe your not changing the contents of arr so you require a call by value instead of a call by address
char array[] - > call by value , char *array -> call by address
Your variable int pointer is not initialized before its use, Its dangerous. First initialize is to zero.
Change all the occurences of *array[pointer] to array[pointer]
If your wishing to change the contents of arr use call by address just change rotate13(char array[]) to rotate13(char *array) and Dont forget to initialize pointer variable to zero .

Pointer to array of structures in C

Please let me know how can I pass a pointer to the array of structures in C as a function argument.
Below is my code.
#include <stdio.h>
#include<strings.h>
typedef struct _Alert
{
char MerchantNo[21];
time_t last_update;
} Alert;
typedef Alert *PALERT;
int set(PALERT palertMerch[5], int *merchnoIndex, char * txnMerchant)
{
strcpy(palertMerch[*merchnoIndex]->MerchantNo, txnMerchant);
*(merchnoIndex) = *(merchnoIndex) + 1 ;
return 0;
}
int main()
{
Alert alert[5];
for(int i =0; i<5;i++)
{
memset(alert[i].MerchantNo, 0x00, 21);
alert[i].last_update = (time_t)0;
}
char *p = "SACHIN";
int index = 0;
set(alert[5], index, p);
}
Error message
"3.c", line 34: argument #1 is incompatible with prototype:
prototype: pointer to pointer to struct _Alert {array[21] of char MerchantNo, long last_update} : "3.c", line 14
argument : struct _Alert {array[21] of char MerchantNo, long last_update}
"3.c", line 34: warning: improper pointer/integer combination: arg #2
cc: acomp failed for 3.c
You just pass the array, it'll get decayed to the pointer to the first array element:
set( alert, &index, p );
Note that I also corrected your second error of passing integer as a pointer for the second argument.
Edit 0:
I missed the declaration of PALERT - your function definition is wrong, it should be something like:
int set( PALERT palertMerch, int* merchnoIndex, const char* txnMerchant )
{
assert( *merchnoIndex >= 0 && *merchnoIndex < 5 );
strcpy( palertMerch[*merchnoIndex].MerchantNo, txnMerchant );
...
}
I know, arrays and pointers are a bit confusing in C, and you were trying to jump to arrays of pointers already :)
You actually cannot pass an array to a function. What happens when you do, is that a pointer to the first element in an array is passed in instead. (That proccess is often described as "an array decays into a pointer").
That is,
set(alert, index, p);
Is just the same as:
set(&alert[0], index, p);
(Note that you called it as set(alert[5], index, p); , this just passes in the 6. element of your array , which btw is invalid, as your array only have room for 5 elements.)
So, what you do when you want to pass an array to a function is you
Pass a pointer to the first element in the array (which can be done by just writing name_of_array or &name_of_array[0]
Add another argument that is the length of the array. You might need this as if your array can have different sizes, and you cannot know how many elements an array have, if all you got is a pointer to its first element:
Let's skip item 2. above for now, you can just do:
//PALERT is already a pointer, otherwise specify the first argument as:
//ALERT *palertMerch
int set(PALERT palertMerch, int *merchnoIndex, char * txnMerchant)
{
strcpy(palertMerch[*merchnoIndex]->MerchantNo, txnMerchant);
*(merchnoIndex) = *(merchnoIndex) + 1 ;
return 0;
}
And call it like:
char *p = "SACHIN";
int index = 0;
set(alert, index, p);
btw, unless you have a good reason, try not to hide a pointer in a typedef as you do in typedef Alert *PALERT; doing so often gets confusing.
remove the array brackets and it should work.
The reason for this is that array notation is an easier way to represent sequences of items in memory. For example, in an array a[5], you can access the third element as a[3] or *(a+3).
Your function takes type PALERT *[5]. You are passing in Alert[5] instead. There are other problems with your code that need fixing before it will successfully run.

Resources