Reputation: 67
Originally, I want to make a program where the user inputs a username and pin. The program checks if both are within the coded structure. If the input is correct, the program displays a certain amount.
But i have ommitted the rest of the code because I always get the error on the isValid.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct accounts{
char unList[50];
int pinList;
float amtList;
}account;
int isValid(char inputUN[], struct accounts);
int main(int argc, char *argv[]) {
account newAccs[10];
char unList[][32] = {"franklin", "woods", "phillips", "gomez", "burns", "porter", "griffin", "spencer", "hanson", "johnson"};
char inputUN[32];
int index;
printf("Enter Username: ");
scanf("%s", &inputUN);
printf("Enter PIN: ");
scanf("%d", &inputPin);
index = isValid(inputUN, newAccs);
printf("%d", index);
return 0;
}
int isValid(char inputUN[], struct accounts acount){
int index;
for(index = 0; index < 32; index++){
if (strcmp(inputUN, acount.unList) == 0){
index = index;
}
}
return index;
}
Note: I do not need the code for my original program. I just want to know why I always get an error: Incompatible type for argument2 of 'isValid'. Also I have shortened my code so for my purpose only.
Upvotes: 0
Views: 1944
Reputation: 881513
newAccs
is an array of those structures, which will decay to a pointer to the first element when passing it to a function.
So, if you want to use a pointer, you'll need your function to accept account *
. If you want a single account element, you'll need to pass one, such as with newAccs[4]
.
Which one you choose will depend on your intent (it's a little difficult to discern which way you want to go based on your current isValid
function at the moment since it doesn't use any array indexing despite the presence of a for
loop).
By way of example, here's a function (which I think is probably what you're after, based on the information provided) that goes through the entire set of accounts and check if the username exists in one of the entries. It will either return the index of that entry or -1 if not found:
int findUN (char *userName, account *accountList, int accountSz) {
int idx;
for (idx = 0; idx < accountSz; idx++)
if (strcmp (userName, accountList[idx].unList) == 0)
return idx;
return -1;
}
Then you call it with something like:
int idx = findUN (inputUN, newAccs, sizeof(newAccs) / sizeof(*newAccs));
You'll notice I've done two things differently there. The first is to accept a pointer to an account structure (the pointer to the first of the ten in the array).
The second is to pass the size of that array, basically the size in bytes of the entire array divided by the size in bytes of one element in that array.
That's because the decay of the array into a pointer does not let you later extract the array size. To do that, you need to pass it separately.
Upvotes: 3
Reputation: 6901
The method isValid
expects as single instance of accounts
as second parameter but you are passing an array to it while calling the method.
So either you need to change the method signature to
int isValid(char inputUN[], struct accounts *);
or pass a single instance of accounts
from the array while calling the method as
isValid(inputUN, newAccs[someindex]);
Upvotes: 0