In this post ( Why the result of this code is the same when the arg is different? ), the parameter of chg
is yay *lol
and the inside of it is lol
(notice there's no asterisk in front of it). But why in this code, it shows up an error?
void chg (int *lol) {lol=9;}
int main ()
{
int a=5;
int *boi=&a;
printf ("%d\n", *boi);
chg (boi);
printf ("%d\n", *boi);
return 0;
}
[Error] invalid conversion from 'int' to 'int*' [-fpermissive]
So, different data types in parameter means it works differently?
In function you should use like this
void chg (int *lol) {
*lol=9;
}
Because it is pointer, it keeps an address. With *
sign, you say that. Go to this address and assign this value.
Also you can use your function like this
int main ()
{
int a=5;
int *boi=&a;
printf ("%d\n", *boi);
chg (&a); //send address of a.
printf ("%d\n", *boi);
return 0;
}
Like this. It means same thing
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.