#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;
}
出力でこのガベージ値を受け取った理由は何ですか?一部のガベージ値が出力に出力されています。理由は何ですか?
あなたはreturn文を追加するのを忘れてしまいました。 return str;
それは唯一の問題ではありません... –
それは唯一の問題ではありませんが、それは問題であり、私はこれがすべきであると指摘されてうれしいです。 – slevy1
あなたはこれを見てしたいことがあります。
#include <stdio.h>
char * getString(void)
{
const * str = "textprint test";
return str;
}
int main()
{
printf("%s\n", getString());
return 0;
}
参照のデモhere
私はconstキーワードなしcodepad.orgで微このスニペットを実行しましたが、このarticle、それは可能性がありますに応じてコンパイラからの警告を避けるために必要です。
コードでは、メモリ内の読み取り専用の場所に格納された文字列リテラルが作成され、ポインタにはそのアドレスが保持されます。 getString()が実行されると、その戻り値はその文字列リテラルへのポインタになります。
によるとarticle前述:
これはOPのコードは次のように変更され、償還であることを示唆している「CおよびC++標準規格は、その文字列リテラルは静的記憶を持っていると言います」 :
#include <stdio.h>
char * getString(void)
{
static char str[] = "textprint";
return str;
}
int main()
{
printf("\n%s",getString());
return 0;
}
を参照してくださいデモhere
この例では、各要素に文字列を含む文字が含まれるような配列が作成されます。配列は、その配列の寿命を大幅に延ばすキーワード "static"の追加を除いて、getString()のスコープ内にのみ存在します。結果として、getString()がmain()で実行されるとき、戻り値はメモリ内にそのまま保持されている配列を指すアドレスです。
ファンクションがprintf()の引数として評価されるためには、getString()にreturn文を含める必要があります。また、という静的アドレスを指定しないと、アドレスは破損したメモリを指し、結果として生ゴミデータを指します。gcc -Wall
を使用してコードをコンパイル
'char * str'が' char str [] 'と異なる理由を追加するのは価値があるかもしれません。なぜなら、作者がそれを見てreturn文を追加するだけかもしれないからです。 –
出力はエディタに依存しますか?オンラインコードエディタで同じプログラムを実行している間は、値は出力されません。リンクはhttp://www.tutorialspoint.com/compile_c_online.php?PID=0Bw_CjBb95KQMMkNXaWNvUjFvNFE –
です。これはエディタではなくCコードをコンパイルして実行するための使い方を理解する必要のある開発環境。がんばろう。あなたが言及したオンラインエディタの@manjunathhiremathを – slevy1
コードの列に定義されている文字列を容易に対するエラーのいくつかの防止安全対策、追加:
#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'は 'char * 'ではなく' int'を必要とします。 – BLUEPIXY
関数getStringは、char *を返しますが、実際に何も返さないことを示しています。あなたのコンパイラは、少なくとも警告を発行しているはずです。さらに、char *を返すときには、ポインタがスタックにない文字列を指していることを確認する必要があります。そうでなければ、関数が戻ると直ちに無効になります。 – mgarey
@mgarey *ポインタがスタック上にない文字列を指していることを確認する必要があります*、私はフレーズの方が良いと思います - ポインタはまだスコープ内にある文字列を指しています(またはローカルではありません呼び出された関数へ)。 C標準にはスタックという概念はありません。 –