printf("%d", p); // is this form of input similar to the one above?
no, %d
expects an int
argument, you're passing an int *
. Supplying mismatched argument type for a conversion specifier invokes undefined behaviour.
That said, in case of
char* name[10]; // array of character pointers!
scanf("%s", name);
printf("%s", name); // runs correctly.
you're going wrong. Check the data types. %s
expects the argument to be a pointer to a char
array, so your code should be
char name[10]; // array of characters
scanf("%9s", name); // mandatory error check for success to be done.
printf("%s", name);
as, in most of the cases including this one, an array type decays to the pointer to the first element of the array, so while passing as function argument, name
is actually of type char *
.
Same goes with
char* names[5][10];
scanf("%s", names[1]);
changing that to
char names[5][10];
will suffice.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…