Reputation: 321
a quick question. I'm trying to write a function which recieves a name of a string and prints its second character, But it won't compile (on http://ideone.com), can you address the issue? I don't see the problem as I am sending an address into the function, and asking to access the character in that address.
#include <stdio.h>
int main(void) {
char line[4] = "abc";
test(line);
return 0;
}
void test(int point) {
point++;
printf("%c",*point);
return;
}
The compile error that I am getting is -
Compilation error time: 0 memory: 10320 signal:0
prog.c: In function ‘main’:
prog.c:5:2: warning: implicit declaration of function ‘test’ [-Wimplicit-function-declaration]
test(line);
^~~~
prog.c: At top level:
prog.c:9:6: warning: conflicting types for ‘test’
void test(int point) {
^~~~
prog.c:5:2: note: previous implicit declaration of ‘test’ was here
test(line);
^~~~
prog.c: In function ‘test’:
prog.c:11:14: error: invalid type argument of unary ‘*’ (have ‘int’)
printf("%c",*point);
^~~~~~
Upvotes: 0
Views: 756
Reputation: 148
1) It wants a function prototype or for the function to be declared above your main.
2) The parameter point in
void test (int point)
is a pointer, not an int.
Upvotes: 0
Reputation: 223729
Two problems here.
First, you use the function test
before it's declared. So the function is implicitly declared as taking an unspecified number of arguments and returning int
, i.e. int test()
. This conflict with the definition void test(int point)
that appears later.
The second issue is that you pass a char
array (which decays to a char *
) to test
, but the function is expecting an int
.
Move the definition of test
to before main
so that is defined before it's used, and change the point
parameter from int
to char *
.
#include <stdio.h>
void test(char *point) {
point++;
printf("%c",*point);
return;
}
int main(void) {
char line[4] = "abc";
test(line);
return 0;
}
Upvotes: 4