1

Im new in C and tryig to split user input string of integers and store them in dynamic table of int[]. If i print out the atoi(token) value - it shows required integer. But when trying to assign it to table - im getting values like below. Im doing something wrong with table, because when trying to pass tabSize to table cells im getting similar result. I will appreciate any insight, i've searched the web but didn't find similar issues.

What i ment to achive is:

//Input:
1,2,3,4

//Output:
tab[0] == 1
tab[1] == 2
tab[2] == 3
tab[3] == 4

What im getting is:
//Output:
tab[0] == 0
tab[1] == 0
tab[2] == 0
tab[3] == 4

Below code im trying:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char *input;
    int *tab;
    int tabSize = 0;

    scanf("%s", &input);

    char *token = strtok(&input, ",");

    while(token != NULL)
    {
      tab = (int*)calloc((tabSize + 1), sizeof(int));

      tab[tabSize] = atoi(token);

      token = strtok(NULL, ",");
      tabSize++;
    }

    for (int i = 0; i < tabSize; i++)
    {
        printf("\n%d", tab[i]);
    }

    free(tab);
    return 0;
}`

Thank you in advance

Casavier
  • 21
  • 3
  • 1
    You never initialised ```char *input```. The pointer should point to a valid memory address either allocated automatically on the stack of dynamically on the heap. – Harith Dec 17 '22 at 18:03
  • 1
    You are calling calloc() each time around the loop, which leaks the previously created array and creates a fresh new one. Maybe you want to be using realloc(). – pmacfarlane Dec 17 '22 at 18:04
  • 1
    Do not cast the result of *alloc functions. Doing so is redundant and can hide errors. See https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Harith Dec 17 '22 at 18:04

1 Answers1

0

Thanks for your replys I've managed to solve the problem :) Calling multiple times malloc() caused to override existing data. I initialized *tab by using malloc() once and then changed the table size by calling relloc().

Here is updated code in case someone will run into similar issue:

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

int main()
{
char *input;
int *tab;
int tabSize = 0;

scanf("%s", &input);

char *token = strtok(&input, ",");

tab = (int*)malloc(1*sizeof(tab[0]));

while(token != NULL)
{
  tabSize++;
  tab = realloc(tab, tabSize*sizeof(tab));

  tab[tabSize - 1] = atoi(token);

  token = strtok(NULL, ",");
}

for (int i = 0; i < tabSize; i++)
{
    printf("\n%d", tab[i]);
}

free(tab);
return 0;
}
Casavier
  • 21
  • 3
  • 1
    **Problems:** The above code doesn't compile for me. ```scanf``` requires a ```char *```, but you gave it a ```char **```. The first argument of ```strtok``` requires a ```char * ```, but you gave it a ```char **```. ```char *input``` is *uninitialised* and doesn't point to a valid memory address. You do not check the return value of ```malloc``` and ```realloc```. Upon failure, you lose all access to the original pointer allocated through ```malloc```, as it now points to ```NULL```. – Harith Dec 17 '22 at 18:41
  • 1
    Writing to uninitialised pointers invoke undefined behaviour in C. – Harith Dec 17 '22 at 18:50