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

void input_all(char* array)
{
    int c = 0;
    int increse = 20;
    int number_of_char = 0;

    for (int increment = 0; (c = getchar()) != '\n'; increment++)
    {
        ++number_of_char;
        if (number_of_char % 10)
        {
            array = (char*)realloc(array, increse + sizeof(char));
            if (array == NULL)
            {
                printf("not alocated!");
                exit(22);
            }
            increse += 10;
        }

        array[increment] = c;
    }
    printf("%s\n", array);
}

int main(void)
{
    char* array = (char*)malloc(10);
    if (array == NULL)
    {
        printf("not alocated\n");
        exit(33);
    }
    input_all(array);

    printf("%s\n", array);
    return 0;
}

So what I'am trying to do is to fill up "array" with getchar. When I try to print it out I get some garbage values at the end (most of the time). I think the problem is that I'am giving out to much space to "array" with realloc but I have no idea how to fix it. I also tried placing all the sizes to 1 in malloc and realloc and increse so that whenever i get a charcter the size of "array" increses but it still did not work. Anyone have any idea how ot fix it? Thanks in advance!

10
  • 2
    you realloc but you don't return the new address Commented Dec 14, 2021 at 17:04
  • for(int increment = 0; (c = getchar()) != '\n'; increment++) will become an infinite loop if your input stream is closed without ever providing a newline. You must also check for EOF. Commented Dec 14, 2021 at 17:12
  • @Ôrel How am I supposed to do that? Could you at least send me a link, I tried looking online but could not find anything useful. Commented Dec 14, 2021 at 17:17
  • @WilliamPursell Well I want to have unlimited number of sentences till '\n' is pressed. Is that a bad thing? Commented Dec 14, 2021 at 17:20
  • just return array; Commented Dec 14, 2021 at 17:21

1 Answer 1

1

array must be null terminated, otherwise printf and other c-string functions don't know where the end of the string is at.

realloc may not return the same address (although that's not an issue on your PC), you have to use the address of pointer.

You can allocate the whole array realloc. If subsequent realloc fails then you don't necessarily have to exit, you can print error and return the string which was already allocated.

void input_all(char** parray)
{
    char* arr = NULL;
    int size = 0, i = 0;
    while(1)
    {
        int c = fgetc(stdin);
        if (c == '\n' || c == EOF)
            break;
        if (i == size)
        {
            size += 10;
            char* temp = realloc(arr, size + 1);
            if (temp == NULL) { printf("realloc failed\n"); break; }
            arr = temp;
        }
        arr[i++] = (char)c;
    }
    if(arr)
        arr[i] = '\0'; //<- don't forget to terminate the string with 0
    *parray = arr;
}

int main(void)
{
    char* array = NULL;
    input_all(&array);
    if (!array)
        return 1;
    printf("%s\n", array);
    free(array);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

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.