2

I am trying to create a C function I can call in small programs I write, to accept user input:

char GetStringMine()
{
    int i = 0;
    char ch;
    char * tmpstring = (char *) malloc(2048 * sizeof(char));
    while(ch != '\n')
    {
        ch = getchar();
        tmpstring[i++] = ch;
    }
    tmpstring[i] = '\0';
    return * tmpstring;
    free(tmpstring);
}

But it does not compile. Please can you tell me what I am doing wrong, and what I can do better?

5
  • Please see this discussion on why not to cast the return value of malloc() and family in C.. Commented Dec 21, 2015 at 11:22
  • 5
    What do you think these two lines return * tmpstring; free(tmpstring); will do? Commented Dec 21, 2015 at 11:23
  • Apparently they make me seem dumber. :-) Commented Dec 21, 2015 at 11:57
  • Every thing happens after return statement wont meter anymore. Commented Dec 21, 2015 at 11:57
  • Thanks! This really helped. Commented Dec 21, 2015 at 12:48

4 Answers 4

2

Finally

But it does not compile

cannot be answered in current form. You need to provide more information in your question to clarify "what" and "how".

Sign up to request clarification or add additional context in comments.

3 Comments

True, but none of these are gonna stop compilation of program.
Sorry. The actual content of comment was changed. May be I pressed some other keys while posting.
@haccks Right sir, that's why I added the last statement in the answer. :)
0

You are trying to return a char pointer(char*) but the return type is a char. Also you should consider other comments too.

char* GetStringMine()
{
    int i = 0;
    char ch;
    char * tmpstring = (char *) malloc(2048 * sizeof(char));
    while(ch != '\n')
    {
        ch = getchar();
        tmpstring[i++] = ch;
    }
    tmpstring[i] = '\0';
    return  tmpstring;
}

3 Comments

No. He is not trying to return char *.
he is, but he does not know that.
He isn't, he's dereferencing the char pointer which evaluates to a char.
0

The worst thing about this code is that you're trying return a pointer to a local variable. The variable tmpstring is destroyed after the execution of your function is complete (i.e. on your return statement).

To correct this, you should ask a char* as a parameter, and store the read characters in it (careful with the overflows if you're going with this solution).

Or, you could declare tmpstring as a static char tmpstring[2048] = {0};. static means it won't be destroyed after the function is over. Although I've seen that kind of things in the standard library sometimes, I wouldn't recommend it since the contents will be erased when the function is called again.

For the other problems, see the previous answers.

Comments

0

Try this...

static char tmpstring[2048] = {0};

char* GetStringMine()
{
    int i = 0;
    char ch = 0;

    while(ch != '\n')
    {
        ch = getchar();
        tmpstring[i++] = ch;
    }

    return tmpstring;  
}
  • You cannot free the allocated memory then return a pointer to it, the data will be gone
  • No need to use malloc, as you are not dynamically allocating the array(1)
  • Return a char*, not a char

(1) In your example you effectively are fixing the size of memory to 2048. Think about protecting against a buffer overrun - what will happen if a user enters more than 2048 characters, and how will you protect your code against this. In a real world application you would need to reallocate if going over the allotted size, or restrict the amount of input for the memory allocated.

2 Comments

Your program is returning a pointer to a local variable (tmpstring) which will also be gone once the function has been called. Correct your answer or you'll certainly get downvotes.
Thanks, how can I dynamically allocate the array? I would like not to set it to fixed length.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.