Reputation: 31
I'm currently writing C program, I'm using struct and pointer in the functions. Everything works well in Windows, but not linux debian. I'm having some errors when I try to compile my program in Linux Debian.
typedef struct human
{
char name[100],code[100];
}human;
void hello(char* name, char* code)
{}
int main()
{
human human;
hello(&human.name,&human.code);
return 0;
}
I get those warning when compiling the main.c file:
Warning passing argument 1 of Human from incompatible pointer type Note: expected char *a but argument is type of char(*)[100] Warning passing argument 2 of Human from incompatible pointer type Note: expected char *a but argument is type of char(*)[100]
Upvotes: 0
Views: 67
Reputation: 6116
The error basically means that you are passing address of an array to a function which expects a char
array.
The type of human.name
is char [100]
i.e. char
array of size 100. When you pass it as human.name
to the function, it decays into base address of that array which has type char*
. So, everything fine, and you actually wanted this behavior only.
Now, you tried to pass &human.name
, which means you are trying to pass address of human.name
array to the function, which is of type char (*) [100]
(a pointer to char
array of size 100), and hence the type mismatch occurs.
Upvotes: 0
Reputation: 137398
typedef struct human
{
char name[100], code[100];
} human;
void hello(char* name, char* code)
{
}
int main()
{
human human;
hello(human.name, human.code);
return 0;
}
Remove the ampersands (&
) when passing the name
and code
arrays. A char
array is compatible with char*
. When you say human.name
, you're really referring to the address of the first element in the array.
Also, if hello
doesn't return anything, its return type should be void
. If you omit the return type, int
is assumed, but this is a very old practice, and highly discouraged.
You should always use at least -Wall
when compiling with GCC. Best practice (which you should get used to sooner rather than later) is:
gcc -Wall -Wextra -Werror foo.c
Finally, use proper indentation!
Upvotes: 4