2016-04-03 13 views
-1

ちょっと私は文字列リテラルのアドレスとコマンドライン引数の開始と終了を表示しようとしています。文字列リテラルとコマンドlind引数のアドレス

int main(int argc, char *argv[]) { 
    printf("Address of argc: %p\n", (void*)&argc); //Is this how u find the address of argc? 
    //How to find out the start and end of command line arguments? 
    printf("Start of argv: %p\n", (void*)argv); //Like this? I am not sure... 

    char* strLiteral = "Hello world"; 
    //how to find the address of "Hello world"? (Address of string literal) 
} 

私は研究しましたが、文字列リテラルのアドレスを取るような回答は許されません...本当ですか?それはどういう意味ですか?文字列リテラルはアドレスを持っていませんか?コマンドライン引数の開始アドレスと終了アドレスも取得する方法を教えてください。 お時間を頂きありがとうございます。

+1

"文字列リテラルのアドレスを取るような回答は許可されていません" - *あなたが聞いたところそれ? **上記住所の場所に**を書くことはできません。これらのアドレスを読み込み専用で使用すると、 'const char *'( '%s '指定子で' printf'のように)を期待する関数にそれらを送ることは不可能になります。もし彼らがあなたの住所を使用できなければ、彼らは価値がないでしょう。 – WhozCraig

+0

ここからhttp://stackoverflow.com/a/12601183/859385 – user859385

+1

あなたはuser6146524に関係がありますか?参照してください:http://stackoverflow.com/questions/36368780/are-weable-to-print-out-the-address-for-string-literal –

答えて

-1

文字列リテラルのアドレスを取ることができます。次のコード、特にメインラインから戻る前の行を見てください。

#include <assert.h> 
#include <stdio.h> 
#include <stdlib.h> 

int main(const int argc, char *argv[]) { 
    assert(argc > 1); 
    printf("argc == %d &argc == %p\n", argc, &argc); 
    printf("argv[0] == %s &argv[0] == %p\n", argv[0], &argv[0]); 
    printf("argv[1] == %s &argv[1] == %p\n", argv[1], &argv[1]); 
    const char *test  = "You can take the address of this"; 
    printf("test == %s  &test == %p\n", test, &test); 
    printf("*address of string literal == %p\n", &"address of string literal"); 
    return 0; 
} 
+4

これから見て、変数テストのアドレスを取ってください。文字列リテラルではありません。私が間違っていれば私を訂正してください。 – user859385

+0

'argv [1]'は文字列の最初の文字のアドレスです。これは、ポインタ値として印刷したいものです。 '&argv [1]'はargvの配列へのアドレスです。全く異なっています。 – selbie

+0

'argv [argc]'は最高で未定義です。 – selbie

関連する問題