Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
I';我试图理解返回本地指针的函数,char数组和char指针_C - Fatal编程技术网

I';我试图理解返回本地指针的函数,char数组和char指针

I';我试图理解返回本地指针的函数,char数组和char指针,c,C,我得到的输出是“pqrs”,但我的理解是我们不能返回本地指针。我开始思考,因为char*t=“pqrs”是字符串文字,它将位于只读内存区域,因此我们可以返回本地指针,但不确定我的理解是否正确 #include <stdio.h> #include <string.h> char *t(char *s1) { char *t = "pqrs"; s1 = "fdsa"; return t; } int main() { char *s =

我得到的输出是“pqrs”,但我的理解是我们不能返回本地指针。我开始思考,因为
char*t=“pqrs”
是字符串文字,它将位于只读内存区域,因此我们可以返回本地指针,但不确定我的理解是否正确

#include <stdio.h>
#include <string.h>

char *t(char *s1)
{
    char *t = "pqrs";
    s1 = "fdsa";
    return t;
}

int main()
{
    char *s = "abcde";
    s = t(s);
    printf("%s \n",s);    
    return 0;
}
#包括
#包括
字符*t(字符*s1)
{
char*t=“pqrs”;
s1=“fdsa”;
返回t;
}
int main()
{
char*s=“abcde”;
s=t(s);
printf(“%s\n”,s);
返回0;
}

您始终可以返回本地指针。您不能做的是返回指向本地数据的指针。但是字符串文字不是本地数据。标准中没有“只读”内存。这与此无关。阅读有关存储持续时间的信息。不管是谁说你不能返回一个本地指针都是完全错误的。不能返回指向超出其作用域的自动变量的指针。那是很不一样的。