1
Ich versuche, einen Zeiger des Argumentvektors zu bilden und den Wert von ihm zu lesen.
Dies funktioniert nicht wie erwartet. Hier ist der aktuelle Code
C - Acces Argumentvektor Erzeuge Zeiger und lese Wert
#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;
}
Die Ausgabe von diesem Code wird als nächstes
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 ?
Was mache ich falsch?
So _what_ Sie erwarten? –
Und ignorieren Sie nicht die Compiler-Warnungen. –
'ArgumentA' ist' char * ', also' & ArgumentA' ist 'char **'. Aber du hast es 'ptr_to_a' zugewiesen, was' char * 'ist. – Barmar