0

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);
              ^~~~~~
4
  • 2
    Why do you use int instead of char* ? Also int can not be dereferenced. Commented Mar 12, 2017 at 3:41
  • 1
    Should be test(char* point) Commented Mar 12, 2017 at 3:42
  • 1
    If you actually read the words in the error message, they're quite specific. You do have to actually read them, though. They're not just there to take up space on the screen; they actually convey information. Commented Mar 12, 2017 at 3:42
  • like this on ideone. Commented Mar 12, 2017 at 4:53

2 Answers 2

4

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.