If you want to point to a string
(array of characters) with a pointer, just do:
ponteiro = string;
So in your code the assignments:
* p = nome;
* p = outronome;
They are not right. Note that *p
means the value pointed to by p
. If p
is a pointer of char
then points to a char
, but both nome
and outronome
are not chars
and yes strings (chars arrays).
Soon to be correct it would have to be:
p = nome;
p = outronome;
As an aside, p
is only used at the end of the program, so the first assignment is not even used.
In the show part, you have the same error here:
printf("%s", *p);
*p
, that is, that indicated by p
is of type char
logo can not be applied with %s
and yes %c
. To be with %s
you must pass a pointer to the first character of the string, which is p
only:
printf("%s", p);
See this example working on Ideone
Complete code corrected for reference:
#include <stdio.h>
void main() {
char *p;
char nome[21], outronome[21];
scanf("%s", nome);
p = nome; //sem *
scanf("%s", outronome);
p = outronome; //sem *
printf("%s", p); //sem *
}