linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Bert Wesarg <bert.wesarg@googlemail.com>
To: 明亮 <longapple@gmail.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: pass a local variable to a function
Date: Wed, 25 Mar 2009 19:50:26 +0100	[thread overview]
Message-ID: <36ca99e90903251150n4508993dvfc1f74c8f1a37d25@mail.gmail.com> (raw)
In-Reply-To: <56b13acf0903250921w1934942bma7280055c97a9db3@mail.gmail.com>

On Wed, Mar 25, 2009 at 17:21, 明亮 <longapple@gmail.com> wrote:
> Hi guys,
>
> This is my first email in this list, any help is much appreciated.
> As I know, it's not allowed to pass a local variable to a function,
> because the stack where local variable resides will be reused by other
> functions.
> eg:
>      1  #include <stdio.h>
>      2
>      3  char *fetch();
>      4
>      5  int main(int argc, char *argv[]){
>      6          char *string;
>      7          string = fetch();
>      8          printf("%s\n", string);
>      9          exit(0);
>     10  }
>     11
>     12  char *fetch(){
>     13          char string[10];
>     14          scanf("%s", string);
>     15          return string;
>     16  }
>
> When the application is executed, after input "a", it will produce
> unknown characters, like "8Šè¿ôÿO". Which is like what I expect
>
> However, if I change line 13 to:
>     13           char string[1024];
>
> When I type "a", it echos "a", which is out of my expectation
>
> Why does it behave like this?
That is irrelevant. What you try to do is 'undefined behavior', so you
should have no expectations, whether it works for you or not.

Bert
>
> Thanks in advance,
> longapple

  parent reply	other threads:[~2009-03-25 18:50 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-25 16:21 pass a local variable to a function 明亮
2009-03-25 17:17 ` Lorenzo Beretta
2009-03-26 13:09   ` Mingliang
2009-03-25 18:50 ` Bert Wesarg [this message]
2009-03-26  8:42 ` Glynn Clements
2009-03-26  9:49 ` Jon Mayo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=36ca99e90903251150n4508993dvfc1f74c8f1a37d25@mail.gmail.com \
    --to=bert.wesarg@googlemail.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=longapple@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).