Thread: Main function does not return any values when calling other function.

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    7

    Main function does not return any values when calling other function.

    Please help. I learning about function with return pointer value.

    The function is supposed to return value from the file in my main, but I am getting empty value.

    I am trying to get better with pointer. Right now just teaching myself. Please help.

    right now the only way for this code to show value is when in put the putchar(*ps) inside my readfile function. I would like to readfile to return value and print in the main function.

    Thanks again everyone..for reading.and helping out.



    Code:
    #include <stdio.h>
    
    
    char *readfile(char filename[]);
    
    
    int main(int argc, char *argv[]  ) {
      char output[400];
      char *pout;
      pout = output;
       
      pout = readfile(argv[1]);
      printf("%c\n", *pout++);
    }
    
    //this is the function that takes file name.... and return pointers.
    char *readfile(char filename[] ) {
      char c;
      char string[400];
      char *ps;
      ps = string;
      FILE *myfile;
      int x;
    
    
      myfile = fopen(filename, "r");
      while ((c=fgetc(myfile)) != EOF){
          *ps = c;
           x++;
    
    
      }
     return ps;
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    ps points to the first character of string, which is a local array and thus no longer exists once control is returned from readfile to the main function. Since you seem to want to store the output in the variable named output, pass output (which will be converted to a pointer to the first character thereof) to readfile as another argument, and use that in place of ps.
    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

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    7
    Thanks Laserlight! Your quick feedback is really helpful. Now, I need to get better on passing local variable to pointer within function.

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Local variables are allocated on the stack and these are "popped" when return is issued. You could allocate your string on the heap (using malloc) and that would still exist when exiting and give you the result you are expecting.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Syscal View Post
    Local variables are allocated on the stack and these are "popped" when return is issued. You could allocate your string on the heap (using malloc) and that would still exist when exiting and give you the result you are expecting.
    If you use malloc() in readfile(), don't forget you should do a free() in main() when done with the buffer.

    You could declare string in readfile() to be static, (static char string[400]; ), which will effectively make string a global variable, but with a local scope (the name for string is local to readfile(), but it's otherwise a "permanent" variable, and you could safely return a pointer to it).

    laserlight's suggestion of passing "output" to readfile() as a parameter is probably the best way to do this.
    Last edited by rcgldr; 06-10-2013 at 04:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to return 2 values upon calling a function
    By David_Baratheon in forum C Programming
    Replies: 1
    Last Post: 05-10-2012, 10:05 AM
  2. Calling this function in my main.
    By filadon in forum C Programming
    Replies: 3
    Last Post: 02-15-2011, 02:58 PM
  3. having some trouble calling a function in the main program
    By CMakesMeSad :( in forum C Programming
    Replies: 19
    Last Post: 06-26-2009, 09:33 PM
  4. calling a function inside main()
    By mero24 in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2005, 01:22 AM
  5. getting a function to return to main??
    By Neildadon in forum C++ Programming
    Replies: 7
    Last Post: 12-16-2002, 10:24 AM