Thread: function that returns string

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    72

    function that returns string

    hi

    i want to make a function that will take for input a number n and a character b

    the function will return a string that contains n times the character b

    i tried this but it doesnt work
    Code:
    char *s(int n, char b) {
         int i;
         char r[n];
         
         for (i=0;i<n;i++){
               r[i] = b;
             }
            
            return r;
    
         
         }
    after some thinking i tried dynamic memory allocation

    Code:
    char *s(int n, char b) {
         int i;
         char *r;
         r = (char *)malloc(n*sizeof(char));
         
         for (i=0;i<n;i++){
               r[i] = b;
             }
            
            return r;
    
         
         }
    and it worked

    can anyone tell me why?

    ty

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Because in the first example, you're returning a pointer to a local variable.
    When the function returns, variable goes out of scope, and you're left with a pointer to junk.
    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.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Because
    Code:
    char r[n];
    is a local stack variable. That means it is part of function s()'s stack, and only accessible locally, that is, inside s(), because each function has it's own stack and cannot access any others.

    Malloc() allocates heap memory, which there is only one heap for the entire program and it is globally accessible.

    When you return a pointer to a local variable, it is useless because the memory addresses used by a function call (a "stack frame") are recycled when the function returns (or else they do not match the addressing used outside of the frame, I'm not sure).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    thanks for both of you, I now understand it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 05-19-2009, 08:17 AM
  2. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Custom String Parsing Function
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-01-2002, 11:07 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM