2017-06-13 35 views
0
#include <stdio.h> 

char *getString() 
{ 
    char str[]="textprint"; 
} 

int main() 
{ 
    printf("%c",getString());  
    return 0; 
} 

在输出中接收垃圾值的原因是什么?某些垃圾值正在输出中打印。是什么原因?

+6

1)'getString'不返回一个值。 2)'printf'的'%c'需要'int',而不是'char *'。 – BLUEPIXY

+2

函数getString表示它将返回一个char *,但实际上并没有返回任何东西。您的编译器应至少发出警告。另外,在返回一个char *时,你需要确保指针指向一个不在堆栈中的字符串,否则一旦函数返回,它指向的数据将立即失效。 – mgarey

+3

@mgarey *您需要确保指针指向一个字符串,它是不是在栈中*,我想那句更好的方式是 - 指针指向一个字符串,它仍然在范围上(或不在本地到被调用函数)。 C标准没有堆栈的概念。 –

回答

-1

您忘记添加返回语句。 return str;

+4

这不是唯一的问题... –

+1

它不是唯一的问题,但它是一个问题,我很高兴这是指出,因为它应该是。 – slevy1

1

你可能想看看这个:

#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()的参数进行计算。此外,没有指定关键字静态,地址将指向损坏的存储器,从而产生垃圾数据。

+2

它也可能是值得的增加,为什么'字符* str'是'字符STR [1]不同'因为作者可能看一下它,并且只需添加return语句。 –

+0

是否输出取决于editor.Because同时在线代码编辑器中运行同一程序没有价值正在printed.The链接在这里http://www.tutorialspoint.com/compile_c_online.php?PID=0Bw_CjBb95KQMMkNXaWNvUjFvNFE –

+1

这不是编辑器,但您需要了解如何使用编译和执行C代码的开发环境。祝你好运。 – slevy1

3
  • 编译你与gcc -Wall代码可以让你几个警告
  • 固定他们(制作“版本1”)让你这会告诉你你的代码
  • 固定,最后警告的核心问题(警告使第2版)让你的工作和安全的代码
  • 我增加了一个选择,打印什么,你可能想打印
  • 我说什么你的函数的目的是 猜测(它没有说服我不猜测)
  • 我添加一种安全预防措施,以防止一些更易于使

代码被静态定义的炭的阵列,串错误:

#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; 
} 
+0

我想在数组的定义中也使用“const”。但是,跳过这一点,解决警告的第一次齐射变化更加突出和明确。 – Yunnosch

+0

欣赏这一努力,但我非常怀疑作者是否会阅读您所写的所有评论。 –

+0

@AjayBrahmakshatriya没有与你相矛盾,我乐观地希望OP会阅读它们。 – Yunnosch