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’re not right. Remember that *p
means the value pointed by p
. If p
is a pointer of char
then points to a char
, but so much nome
as outronome
are not chars
and yes strings (arrays of chars).
Soon to be correct it would have to be:
p = nome;
p = outronome;
As an aside, the p
is only used at the end of the programme, so that the first allocation is not even used.
In the show part, there’s the same mistake, here:
printf("%s", *p);
*p
, that is, the pointed by p
is the type char
so can not be applied with %s
and yes %c
. To be with %s
has to pass a pointer to the first character of the string, which is the 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 *
}
Yes, I forgot that, I updated my answer
– Carlos Henrique