1
引数ベクトルのポインタを作成し、そこから値を読み取ろうとしています。
これは期待どおりに機能しません。ここでは、現在のコード
C - Acces引数ベクトルの作成ポインタと読み込み値
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *ptr_to_a;
if(argc != 3)
{
return -1;
}
char* argumentA = argv[1];
char* argumentB = argv[2];
ptr_to_a = &argumentA;
printf("First argument is %s \n", argumentA);
printf("Second argument is %s \n", argumentB);
printf("Memory address of ptr_to_a is %p \n", ptr_to_a);
printf("Memory address of argumentA is %p \n", &argumentA);
printf("First argument through pointer is %s \n", *ptr_to_a);
return 0;
}
このコードの出力は
First argument is hello
Second argument is world
Memory address of ptr_to_a is 0x7fff5744cb80
Memory address of argumentA is 0x7fff5744cb80
First argument through pointer is ?
私が間違って何をやっているように次のですよ?
だから、あなたは_what_を期待していますか? –
コンパイラの警告を無視しないでください。 –
'argumentA'は' char * 'なので、'&argumentA'は 'char **'です。しかしそれを 'ptr_to_a'に割り当てました。これは' char * 'です。 – Barmar