Thread: function returning String

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    12

    function returning String

    I'm writing a function that will be called everytime the program ask for the user to input something.
    Code:
    int main(voidz)
    {
        char integer[INT_SIZE+EXTRA_SPACES];
        int number;
        printf("Enter a number.");
        *integer=getInput(INT_SIZE);
        number=atoi(integer);
        printf("%i",&integer);
    
    
    }
    
    
    
    
    
    
    void readRestOfLine()
    {
       int c;
    
    
       /* Read until the end of the line or end-of-file. */
       while ((c = fgetc(stdin)) != '\n' && c != EOF);
    
    
       /* Clear the error and end-of-file flags. */
       clearerr(stdin);
    }
    
    
    char* getInput(int size)
    {
        char input[size+EXTRA_SPACES];
        fgets(input,size+EXTRA_SPACES,stdin);
        if (input[strlen(input) - 1] != '\n')
        {
            /* String was too long. Reject string and flush input buffer. */
            printf("Input was too long.\n");
            readRestOfLine();
        }
        else
        {
             input[strlen(input) - 1] = '\0';
             return input;
        }
    }
    My problem is,it's not printing the number entered,but the memory location(i think).And also,when a invalid(over-length) input,it still print out the memory location.
    Thanks.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I don't know who taught you to read from a file like that but it's just mental.

    On another note "integer" is a char array and you are printing its address. Google how to printf a char array in C.

    Also, int main(VOIDZ????) Hahaha.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    12
    oops,sorry about the error up there.
    Code:
    int main(void)
    {
        char integer[INT_SIZE+EXTRA_SPACES];
        int number;
        printf("Enter a number.");
        *integer=getInput(INT_SIZE);
        number=atoi((void*)integer);
        printf("%s",integer);
        printf("%i",number);
    
    
    }
    why it's not working for the both printf?

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    number=atoi((void*)integer);

    What the hell are you trying to achieve here?

    Also, is integer correctly terminated?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You should pass char-Array to your function, you should never return an lokal automatic variable to the calling context, try:
    Code:
    int main(void)
    {
        char integer[INT_SIZE+EXTRA_SPACES];
        int number;
        printf("Enter a number.");
        getInput(INT_SIZE+EXTRA_SPACES,integer);
        number=atoi(integer);
        printf("%i",number);
    }
    
    void readRestOfLine()
    {
       int c;
    
    
       /* Read until the end of the line or end-of-file. */
       while ((c = fgetc(stdin)) != '\n' && c != EOF);
    
    
       /* Clear the error and end-of-file flags. */
       clearerr(stdin);
    }
    
    
    char* getInput(int size,char *input)
    {
        fgets(input,size,stdin);
        if (input[strlen(input) - 1] != '\n')
        {
            /* String was too long. Reject string and flush input buffer. */
            printf("Input was too long.\n");
            readRestOfLine();
        }
        else
        {
             input[strlen(input) - 1] = '\0';
             return input;
        }
    }

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You should be compiling with warnings enabled. This:

    Code:
       char input[size+EXTRA_SPACES];
    is a local variable. You can't return those because they go out of scope when the function ends. Ie, they don't really exist. They may seem to exist, but they are prone to getting overwritten, in whole or in part, by something else (if you used the compiler warnings, you would know this).
    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

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    12
    thanks~it's working now....^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  2. returning a string from a function
    By budala in forum C Programming
    Replies: 4
    Last Post: 01-18-2010, 10:03 PM
  3. Returning a string from function
    By badmantel in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 12:17 PM
  4. returning a string from a function
    By revelation437 in forum C Programming
    Replies: 6
    Last Post: 12-14-2002, 04:21 AM
  5. returning a string from a function
    By itld in forum Linux Programming
    Replies: 5
    Last Post: 12-03-2001, 01:35 AM