-1

I am starting to learn pointers in C.

Why do I have an error in line 8 at &i?

This is the source:

char * func(char *d, char *str) 
{
    return d;
}

int main(int argc, char *argv[])
{
    char *i = NULL;
    func(&i, "aaa"); // line 8. here I have the error (in "&i")
}
Sadique
  • 22,572
  • 7
  • 65
  • 91

4 Answers4

3

You are passing char** and the function expects a char*. You need to pass i without the address of & operator, because this way you are taking the address of the pointer.

Just pass func(i, "aaa");

concept3d
  • 2,248
  • 12
  • 21
1

The type of &i is not char * it is char * *.

You should go through this How do pointer to pointers work in C?

Community
  • 1
  • 1
Sadique
  • 22,572
  • 7
  • 65
  • 91
1

When you write &i, you are passing the memory address of i, but since i is already a char*, the type of &i is char** (pointer to pointer to char). You therefore have an extraneous & that is causing a type mismatch. Simply remove the & and pass i to the function with taking its address:

func(i, aaa); //no need to use & on a variable that is already a pointer
ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
0

As per your function prototype func(char *d, char *str) first parameter accepts pointer of char type and in char *i; i is a pointer of char type so pass it directly to function.

If you want to pass &i to the function you need to have pointer to pointer variable that is char **d.

eatSleepCode
  • 4,427
  • 7
  • 44
  • 93