Thread: Returning a character pointer and assigning it to another

  1. #1
    Registered User
    Join Date
    May 2015
    Location
    Lucknow, U.P., India
    Posts
    9

    Returning a character pointer and assigning it to another

    I am trying to write a program that prints Fibonacci series of characters by replacing 0 with 'a' and 1 with 'b' and next characters accordingly. I am using another function to recursively find out a particular Fibonacci term and return it as a character pointer.
    Code:
     #include <stdio.h>#include <string.h>
    #include <stdlib.h>
    char* FiboS(int);
    void D_e() {
        printf("Enter fibbonacci series limit\n");
        int inp;
        scanf("%d", &inp);
        char *fibs;
        //fibs = (char *)malloc(11);
        fibs= FiboS(inp - 1);
        printf("%s\n", fibs);
    }
    char* FiboS(int i) {
        if (i == 0)
            return "a";
        else if (i == 1)
            return "b";
        char st[10];
        st[0] = '\0';
        strcat(st, FiboS(i - 1));
        strcat(st, FiboS(i - 2));
        //printf("%s\n", st);
        return st;
    }
    I used printf() function in FiboS() function to ensure that it was returning proper value and it showed that it was working well.
    But the code is giving wrong output, the string stored in char pointer fibs is not the same as returned by FiboS() function. I thought it was because fibs was not allocated any memory so I used malloc() but it made no difference. So I used strcpy() function and it worked.
    But I don't understand why fibs was not storing the proper value and what strcpy() does different that it works?

    Excuse me if I've missed something obvious, I'm a beginner.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    on line 23 you return pointer to local variable which will be invalid as soon as function returns.

    So instead - pass the buffer into FiboS function and fill it with data you want to return, make sure you do not go out of array bounds
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    May 2015
    Location
    Lucknow, U.P., India
    Posts
    9
    Quote Originally Posted by vart View Post
    on line 23 you return pointer to local variable which will be invalid as soon as function returns.

    So instead - pass the buffer into FiboS function and fill it with data you want to return, make sure you do not go out of array bounds
    Thanks for replying, but does it mean that functions in C cannot return any value using local variables? I used them because this works in java.

  4. #4
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by amit_s95 View Post
    Thanks for replying, but does it mean that functions in C cannot return any value using local variables? I used them because this works in java.
    You can return using local variable but you cannot return pointers to local variables - the memory pointed to goes out of scope when the function returns hence the pointer becomes invalid. On the other hand, if you return the variable itself, it is copied over.

  5. #5
    Registered User
    Join Date
    May 2015
    Location
    Lucknow, U.P., India
    Posts
    9
    Quote Originally Posted by Aslaville View Post
    you cannot return pointers to local variables
    does it include global variables too?
    And if its true, how can we return a string in C? Using a global variable?
    And if FiboS() is not returning anything valid then how is strcpy() working? Besides, strstr() does return a char pointer, right?
    Thanks for answering.

  6. #6
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    You need to understand global and local variables first - at least with regards to C language - that will answer you question on those library functions.

    Local variabes mainly refers to variables declared on the stack - that means any variables declared within a function without manually allocating memory.

    You can also have local variables if you declare variables within a particular block.
    Last edited by Aslaville; 05-19-2015 at 03:14 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    amit_s95: if you only want to print, do you really need to generate a string explicitly? It seems to me that you could just implement the function as you would with ints, then convert for printing. You should also consider that the range of char is likely to be smaller than the range of int, and even more problematic is that you would run out of letters in the English alphabet very quickly.

    Quote Originally Posted by Aslaville
    Local variabes mainly refers to variables declared on the stack - that means any variables declared within a function without manually allocating memory.
    I would rather talk about "scope": local variables are variables with block scope, declared within the body of a function. vart was talking about non-static local variables, but static local variables are possible too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with assigning character arrays
    By KSG XII in forum C Programming
    Replies: 7
    Last Post: 04-05-2015, 04:04 PM
  2. fgetc assigning input to \n character
    By Yendall in forum C Programming
    Replies: 6
    Last Post: 04-08-2014, 09:48 PM
  3. Replies: 1
    Last Post: 01-07-2013, 10:53 AM
  4. Assigning a pointer a value -- Still = 0?
    By rjg32 in forum C Programming
    Replies: 1
    Last Post: 02-05-2009, 12:16 AM
  5. Replies: 9
    Last Post: 09-19-2007, 02:26 AM

Tags for this Thread