我尝试了以下代码,但遇到了错误:冲突类型很有趣。是否有不需要使用malloc的解决方案。
#include <stdio.h>
int main()
{
printf("%s",fun());
return 0;
}
char* fun()
{
static char str[]="Hello";
return str;
}
答案 0 :(得分:4)
这是因为您尚未声明services.AddIdentity<ApplicationUser, IdentityRole>()
的原型。
services.AddIdentity<IdentityUser, IdentityRole>()
答案 1 :(得分:0)
C不允许从函数返回数组,但允许从函数返回struct
。您可以定义struct
类型以将字符串保存在数组中,然后从函数中返回这样的struct
并将其复制到接收方struct
中:
#include <stdio.h>
struct String
{
char body[1024];
};
struct String fun(void);
int main(void)
{
struct String my_string = fun();
printf("%s\n", my_string.body);
return 0;
}
struct String fun(void)
{
return (struct String){ .body = "Hello" };
}
答案 2 :(得分:-1)
char* fun()
{
static char str[]="Hello";
return str;
}
str保留字符串的基地址。 (假设1000)。现在,当您返回str 时,它将仅返回基地址。
printf("%s",fun());
在这里您要打印字符串,所以您给了%s,但是这个有趣的返回字符数组(字符串)的基地址,而不是字符串(如您假设的那样)。
首先,您需要在printf中取消引用fun(),以便它将字符串数组的第一个字符作为str给出指向字符串的第一个字符的基地址。
此外,您需要将格式程序指定为%c ,以便将其设置为 H 。
现在要打印整个字符串,您需要增加char指针中的内容。
请参见下面的代码:
#include <stdio.h>
char* fun();
int main()
{
int i;
for(i=0;i<6;i++){
printf("%c",*(fun()+i));
}
return 0;
}
char* fun()
{
static char str[]="Hello";
return str;
}
在这里您可以看到我首先取消引用fun()以打印第一个字符,然后进行了for循环,以便可以使用循环变量i来递增fun()返回的指针中的内容。
尝试并让我知道您是否在这里遇到任何问题。