Thread: String returned from function being truncated for some reason -- any idea why?

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    2

    String returned from function being truncated for some reason -- any idea why?

    I have a C program and in main(), a function is called that returns a string. The string is a long string 600+ chars long, but it only returns the first 240 characters of that string. How can I fix this? Your help is most appreciated Thanks!

    Here is the execution of the program:

    % a.out
    12345678901234567890123456789012345678901234567890 12345678901234567890123456789012345678901234567890 12345678901234567890123456789012345678901234567890 12345678901234567890123456789012345678901234567890 1234567890123456789012345678901234567890

    ... piped into word count it counts 242 characters (probably including CR, LF)...
    % a.out | wc
    1 1 242

    Here is the function...

    Code:
    /*----------------------------------------------------------------------------*/
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    /*---------------------------------------------------------
       main program start here
    ---------------------------------------------------------*/
    
    main(argc, argv)
    int argc;
    char *argv[];
    {
       char str_val[800];
    
       memset(str_val,'\0',800);
    
       sprintf(str_val, "%s", getstr2());
       printf("%s\n",str_val);
       exit(0);
    } /* main */
    
    int getstr2()
    {
      char tmp_str[800];
      int i;
      
       memset(tmp_str, '\0', 800);
       sprintf(tmp_str, "%s", "1234567890");
       for ( i=0; i<59; i++)
          strcat(tmp_str, "1234567890");
       strcat(tmp_str, "xyz.");
       return(tmp_str);
    }

  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
    There are many things wrong with your program.

    1. main(argc, argv) int argc; char *argv[];
    This style of function definition (also known as K&R style) has been obsolete since 1989.

    2. sprintf(str_val, "%s", getstr2());
    ...
    int getstr2()
    You use the result as a string, but then go on to declare it as returning an int.

    3. return(tmp_str);
    tmp_str is a LOCAL variable, and you're basically returning a pointer to that local variable.
    When that variable goes out of scope, the pointer is now pointing to someone else's memory.


    Pass str_val as a parameter to your function, then fill it in with data.
    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
    Registered User
    Join Date
    May 2012
    Posts
    2
    Worked.

    Thanks for the reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-23-2011, 09:18 PM
  2. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  3. Replies: 16
    Last Post: 09-23-2008, 03:32 AM
  4. Replies: 7
    Last Post: 09-02-2005, 04:40 AM