0

I started working on a program and I need to create function, that reads input, in format {number,number, ...} and puts numbers into dynamically allocated array. I am currently having trouble reallocating the array. Here is the code. The program compiles with no errors or warnings

int readInput(int ** znamky, int *count )
{
    size_t n = 1;
    char overeni[2]; 
    char znaminko[2];
    char carka [2] = ",";  
    char zavorka[2] ="{";
    char zavorka2[2] ="}";

    printf("Pocty bodu:\n");
    scanf(" %c",&overeni[0]); // I check if first character is { 
    if (overeni[0]!=zavorka[0])                       
    {
        printf("Nespravny vstup.\n");
        return EXIT_FAILURE;
    }
    while (scanf("%d",znamky[*count])==1)  // here I enter the cycle and load first number
    {
       if (scanf(" %c",&znaminko[0])==1) // if char after number is ","
        {
            if (znaminko[0]==carka[0])
                {
                    printf("sdf\n");
                    *count = *count + 1; // I append lenght of array
                    if (*count==n) // if the length is same as currently allocated memory
                    {
                        n = n*2; 
                        *znamky = (int*) realloc (*znamky, n * sizeof(int) + 4); 
// Here I am trying to reallocate the memory and when using debugger it probably goes ok, but //the problem is when I repeat the cycle for the second time because I get seg fault
                    }
                    continue;
                }
            if (znaminko[0]==zavorka2[0])
            {
                *count = *count + 1;
                printf("utikam\n");
                return 1;
            }
        }
        else 
        {
            printf("Nespravny vstup\n");
            return EXIT_FAILURE;
        }
    }
return 1;
}
int main (void)
{
 int *znamky = (int*) malloc (sizeof(int));
    int count = 0;

    if (znamky == NULL)
        {
        free (znamky);
        return EXIT_FAILURE;
        }   

    readInput(&znamky, &count);
    return 0;
}

So I was wondering if someone could explain to me what is happening inside the program, I still feel a little bit lost with pointers, and passing arrays to functions. Thank you very much...

  • It's not related to your problem, but in `main()`, if `znamky` is `NULL`, calling `free()` is unnecessary. `free(NULL)` is a no-op. – sj95126 Nov 26 '22 at 16:55
  • Aside: These comparisons are rather clumsy: `znaminko[0]==carka[0]`, etc. They make this program hard to read, and the variables have no reason to be arrays. `carka`, `zavorka`, and `zavorka2` would be better served as simple [character constants](https://en.cppreference.com/w/c/language/character_constant), defined as clear tokens, and `overeni` and `znaminko` could just be a single `char`. i.e., `#define START '{'` ... , `char overeni;`, `scanf(" %c", &overeni)`, `if (START != overeni)`, and so on. – Oka Nov 26 '22 at 17:02
  • Unrelated, but do not the cast the result of malloc. See this SO answer: https://www.google.com/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc%23:~:text%3DYou%2520don%27t%2520cast%2520the%2520result%2520of%2520malloc%2520%252C%2520because%2520doing,how%2520the%2520C%2520language%2520works.&ved=2ahUKEwjunMbBqcz7AhXyg_0HHcwJAVwQFnoECAsQBQ&usg=AOvVaw0vxdT9A62leLHRNNS3dFtW. You've declared the return type of the function as int, and you return 1, but main makes no use of the returned int. Consider changing it to void. – Harith Nov 26 '22 at 17:03
  • Refer to the man page for realloc https://linux.die.net/man/3/realloc – Harith Nov 26 '22 at 17:08
  • Just like any other variable, if you want it to change from another function, you have to pass it's address as argument, if you want to change a pointer, send the address of the pointer (pointer to pointer) to the function. To realloc an array, send the address of the variable containing the address of the first element (&array), otherwise only local copy will be re-allocated, causing memory leak when function ends. – AR7CORE Nov 26 '22 at 20:58
  • `int * arr = malloc(); my_func(& arr);` and in `my_func` you do `* arr = realloc();` – AR7CORE Nov 26 '22 at 20:59

0 Answers0