Re: pass a local variable to a function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Mar 25, 2009 at 17:21, 明亮 <longapple@xxxxxxxxx> 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 youshould have no expectations, whether it works for you or not.
Bert>> Thanks in advance,> longapple��.n��������+%������w��{.n�����{��ji���^n�r������&��z�ޗ�zf���h���~����������_��+v���)ߣ�m


[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux