Thread: How array store in stack ..?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    How array store in stack ..?

    Hello all,

    I stuck to a problem

    Statement: i define two function one returning a string and other a single char.

    the code is work fine it successfully return string "martin" and a char 'a'....

    Code:
    char *func(void)
    {
         static char Text2[10]="martin";
         return (Text2);        
    }
       
    char* funchar(void)
     {
            char ch;
            
            ch = 'a';
            return &ch;
     }
    
    main()
       {
          char *Text1;
          char *Text2;
          char* c;
          Text1 = func();
         
          printf("\ntext1:%s",Text1);
              
          Text2 = "outside";
          printf("\ntext2:%s",Text2);
          
          c = funchar();
            printf("\nc:%c",*c);
    
                          
          getch();
       }
    but if i remove the word static from func(),like,

    Code:
    char *func(void)
    {
         char Text2[10]="martin";
         return (Text2);        
    }
    than this function return a garbage but as in funchar() i didn't declare char ch as static still it return 'a'.

    Why this is happen..?

    Do array whether it is a char array or and other(int ,double) , only its location is stored in stack means actually the string "martin" stored somewhere else but its reference will stored in stack.

    Or the whole string "martin" will stored in stack.?

    Thanks,
    Gunjan

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Question 7.5a
    string literal like "helo"are stored not in stack.
    So it is valid to say return "hello".
    But don't get confused with array initialization.a[]="hello". Which is same as a[]= { 'h','e','l','l','o',\0'}
    Last edited by Bayint Naung; 03-09-2011 at 04:09 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    funchar() is broken in the same way that func() is broken, when you remove the static.

    In both cases, you've got a pointer to a local variable that has gone out of scope.

    The memory is still there (most likely), and it may indeed print the value you expected, but the code is surely broken.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    Thanks all for their quick inputs..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Array Stack Problem
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2001, 06:58 PM