0

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++;
    }

}
3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by gerard4143
Featured Replies
  • 1
    Aia 1,977   7 Years Ago

    [QUOTE=gerard4143;1047847]This function returns the address of the local variable str [/QUOTE] Variable str is created when the function leer() is used. It is [1]destroyed when the function leer() is finished. You are returning a pointer to that memory that might or might not contain the previous declared value. [1] nothing … Read More

0

This function returns the address of the local variable str

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;
}

Edited by gerard4143: n/a

1

This function returns the address of the local variable str

Variable str is created when the function leer() is used. It is [1]destroyed when the function leer() is finished. You are returning a pointer to that memory that might or might not contain the previous declared value.

[1] nothing is destroyed, it is just released as part of leer() and now it is available to reuse.

0

This function returns the address of the local variable str

Variable str is created when the function leer() is used. It is [1]destroyed when the function leer() is finished. You are returning a pointer to that memory that might or might not contain the previous declared value.

[1] nothing is destroyed, it is just released as part of leer() and now it is available to reuse.

Oook!! Now that makes a lot of sense, thank you. That's why it only printed the first char. Grrr i should have known.

Mental note: next time give more credit to debug's warnings.

0

This function returns the address of the local variable str


Oook!! Now that makes a lot of sense, thank you. That's why it only printed the first char. Grrr i should have known.

Mental note: next time give more credit to debug's warnings.

I'm not sure which compiler your using but you should engage your warnings...i.e. gcc has a switch setting -Wall which is "warn all" and it picked up this problem in the compile stage..

Edited by gerard4143: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.