gpt4 book ai didi

c - 返回指针的函数

转载 作者:太空宇宙 更新时间:2023-11-04 06:39:38 26 4
gpt4 key购买 nike

我在 c 中发现了一个程序:

int bags[5]={20,5,20,3,20};
int main()
{
int pos=5,*next();
*next()=pos; //problem with this line(should give error :lvalue required)
printf("%d %d %d",pos,*next(),bags[0]);
return 0;
}
int *next()
{
int i;
for(i=0;i<5;i++)
if(bags[i]==20)
return (bags+i);
printf("error");
exit(0);
}

the output to the program is 5 20 5 though i expected it to be lvalue required. can anyone tell the reason???

最佳答案

虽然 next() 不是左值,但 *next() 是,这是一个 int,如果 *next()(这是 bags` 数组中的一个整数)。

换句话说,虽然您不能对函数的返回值进行赋值,但您可以对返回值寻址的值进行赋值。

关于c - 返回指针的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10261910/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com