Hi, i'm facing a weird behavior in this code that i just wrote. It was supposed to read a string from stdin and just write it out.
I've debugged it and for me it reads perfectly, but in the while (line 38), *p just seems to be taking weird chars although it's pointing to the correct mem address (checked with those printf i put).
What's going on?
#include <stdio.h>
#define LONG 100
char * leer();
void printar(char *);
int main()
{
printf("Insert a string: ");
//char *str = leer();
printar(leer());
return 0;
}
char * leer()
{
char str[LONG], *p;
int c;
p = str;
while((c = getchar()) != EOF)
{
if(c == '\n')
{
*p = 0;
break;
}
*p = c;
printf("p = %p, *p = c = %c\n", p, *p);
p++;
}
return str;
}
void printar(char *str)
{
char *p = str;
while(*p != 0)
{
printf("p = %p, *p = %c\n", p, *p);
putchar(*p);
p++;
}
}