1

I am having trouble with a function that should read a string from the user. I am always getting (null) as the output.

Is this even a "right" approach for that kind of problem?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int getString(char *input);

int main(void)
{
        char *arr = NULL;
        printf("please enter string: ");
        getString(arr);
        printf("%s", arr);
        return 0;
}

int getString(char *input)
{
        int i;
        char c;
        char *tmp;
        input = malloc(sizeof(char));
        for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i) {
                tmp = realloc(input, (i + 2) * sizeof(char));
                if (tmp == NULL) {
                        free(input);
                        printf("allocation error");
                        return -1;
                }
                input = tmp;
                input[i] = c;
        }
        input[i] = '\0';
        return 0;
}

2 Answers 2

1

If you want to dynamically allocate the string you need to pass a pointer to char*, not just a char *. This way, the function can modify the real char * pointer and the caller will see the result. In your current code, the input variable only exists inside the function and does not affect the variable used by the caller, therefore your arr stays unchanged (NULL).

Something like this:

int getString(char **input)
{
        int i;
        char c;
        char *tmp, *cur = NULL;

        // No initial malloc() needed here.
        // Let realloc() do the job passing NULL the first time.

        for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i) {
                tmp = realloc(cur, (i + 2) * sizeof(char));
                if (tmp == NULL) {
                        free(cur);
                        printf("allocation error");
                        return -1;
                }
                cur = tmp;
                cur[i] = c;
        }

        cur[i] = '\0';
        *input = cur;
        return 0;
}

And then pass the parameter like this:

getString(&arr);
Sign up to request clarification or add additional context in comments.

2 Comments

Don't I have to put an free(cur); before the return 0; to prevent a memory leak?
@sneeed absolutely not, you give the memory to the caller (*input = cur). If you free before returning everything becomes invalid.After the function return it's the caller's job to use the data and free it when it's done.
0

You should return the input pointer, since it is local to your function and get deallocated when the program leaves from the function, so in main the arr is still NULL.

int* getString(char *input);

int main(void)
{
        //...
        arr = getString(arr);
        //...
}

int* getString(char *input)
{
        //...
        return input;
}

1 Comment

Tanks for your explanation!

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.