#include <stdio.h>
char *getString()
{
char str[]="textprint";
}
int main()
{
printf("%c",getString());
return 0;
}
在输出中接收垃圾值的原因是什么?某些垃圾值正在输出中打印。是什么原因?
#include <stdio.h>
char *getString()
{
char str[]="textprint";
}
int main()
{
printf("%c",getString());
return 0;
}
在输出中接收垃圾值的原因是什么?某些垃圾值正在输出中打印。是什么原因?
你可能想看看这个:
#include <stdio.h>
char * getString(void)
{
const * str = "textprint test";
return str;
}
int main()
{
printf("%s\n", getString());
return 0;
}
观看演示here
我跑这个片段在精而不codepad.org const关键字,但根据本article它可能有必要避免编译器的警告。
该代码创建一个字符串字面值,它存储在内存中的只读位置,指针保存其地址。当getString()执行时,它的返回值是一个指向该字符串文字的指针。
根据上述article:
“C和C++标准说字符串文字具有静态存储”
这表明OP的代码是可赎回,修改如下:
#include <stdio.h>
char * getString(void)
{
static char str[] = "textprint";
return str;
}
int main()
{
printf("\n%s",getString());
return 0;
}
观看演示here
在这个例子中,创建的是一个数组,每个元素包含一个包含字符串的字符。该数组只会存在于getString()的范围内,除了添加关键字“static”,这大大延长了该数组的寿命。因此,当getString()在main()中执行时,返回值是一个指向数组在内存中保持不变的地址。
请注意,getString()必须包含一个返回语句才能使函数作为printf()的参数进行计算。此外,没有指定关键字静态,地址将指向损坏的存储器,从而产生垃圾数据。
它也可能是值得的增加,为什么'字符* str'是'字符STR [1]不同'因为作者可能看一下它,并且只需添加return语句。 –
是否输出取决于editor.Because同时在线代码编辑器中运行同一程序没有价值正在printed.The链接在这里http://www.tutorialspoint.com/compile_c_online.php?PID=0Bw_CjBb95KQMMkNXaWNvUjFvNFE –
这不是编辑器,但您需要了解如何使用编译和执行C代码的开发环境。祝你好运。 – slevy1
gcc -Wall
代码可以让你几个警告代码被静态定义的炭的阵列,串错误:
#include <stdio.h>
// version 2
// this is a change done after all other warnings were resolved,
// if you try it yourself, keep the version 1 (see below) to see the
// enlightening warning
char str[]="textprint";
// example for "localisation" strings
char strFr[] = "imprimertexte";
const char *getString(void) // better signature
// (with void as parameter list) for function, for good practice;
// the keyword "const" prevents changing the global string,
// which is a safety precaution
{
// version 1 char str[]="textprint";
// this version 1, after all the other warnings are resolved,
// gets the enlightening warning
// "function returns address of local variable",
// so this got moved somewhere else for version 2
// Returning a pointer to a global variable/text from a function
// is not obviously useful - at first glance.
// One reason could be the design to be able to return different
// pointers to different strings, depending on some condition.
// I can imagine for example localisation of the texts.
// That could look like
// if(localisation == English) {return str;}
// else {return strFr;}
return str; // to avoid warnings
// a) "unused variable"
// b) "control reaches end of non-void function"
}
int main(void) // better signature for main, good practice
{
printf("%c",*getString()); // change what to print, in order to avoid warning
// "format '%c' expects type 'int', but argument 2 has type 'char *' " ,
// by changing from "pointer to char" to
// "what a pointer to char points to", i.e. "char"
printf("\n"); // print a newline for better readable output
// Alternative, for printing the complete text:
printf("%s\n", getString());
// this attempts to change the global string
// with the "const" keyword at the function head this does not work
// it gets: "error: assignment of read-only location '*getString()'"
// *getString()='T';
return 0;
}
1)'getString'不返回一个值。 2)'printf'的'%c'需要'int',而不是'char *'。 – BLUEPIXY
函数getString表示它将返回一个char *,但实际上并没有返回任何东西。您的编译器应至少发出警告。另外,在返回一个char *时,你需要确保指针指向一个不在堆栈中的字符串,否则一旦函数返回,它指向的数据将立即失效。 – mgarey
@mgarey *您需要确保指针指向一个字符串,它是不是在栈中*,我想那句更好的方式是 - 指针指向一个字符串,它仍然在范围上(或不在本地到被调用函数)。 C标准没有堆栈的概念。 –