[英]C: Simple pointer question
I want to program an access function that returns username and password.我想编写一个返回用户名和密码的访问 function。 Here's what I came up with:
这是我想出的:
#include <stdio.h>
char *
getMySQLUsername()
{
return "myUsername";
}
char *
getMySQLPassword()
{
return "myPassword";
}
int main()
{
printf("%s\n", getMySQLPassword());
}
It seems to work, but is this code correct?它似乎有效,但这段代码是否正确?
You should return const char *
because you cannot change a literal string.您应该返回
const char *
因为您不能更改文字字符串。 You're also not returning anything from main
, which is only valid in C as of C99 and in C++.您也没有从
main
返回任何内容,这仅在 C99 起的 C 和 C++ 中有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.