I have created the pointer, and allocated memory to it. This memory should contain the number, that I'm passing inside in array style (1 number = 1 element), also, the 0 element is the size of the number. However, I'm facing the problem of changing data inside the allocated memory, using a pointer. If I'm using my code in the main function, everything goes perfect, but when I created another function and passed a pointer value inside, I can't save it back (if the number is bigger then 99999 and takes 6 elements to be stored).
The main function:
int *convNum=NULL;
int main() {
int num;
convNum = malloc(sizeof(int));
if (convNum==NULL){
printf("Allocation error\n");
exit(0);
}
scanf("%d", &num);
convNum[0] = numLength(num);
printf("%d\n", convNum[0]);
convertNum(num, convNum);
for (int i = 1; i<=convNum[0]; i++){
printf("%d", convNum[i]);
}
printf("\n");
return 0;
}
The function, that I'm trying to use
int convertNum(int num, int * convNum){
int temp = num;
int size = convNum[0];
convNum = realloc(convNum, sizeof(int)*(size+1));
if (convNum==NULL){
printf("Allocation error\n");
exit(0);
}
for (int i=size;i>0;i--){
convNum[i] = temp % 10;
temp = temp / 10;
}
return 0;
}
EDITED The output with the number size less, than 6 gives me the result I needed. The output with other numbers gives me broken size (something like -10349945820) and no proper result.