I'm trying to create a dynamic array program which is implemented using structs, which struct members are passed into functions, and functions changes the values for the variables being passed to it.
The problem is, I'm not getting the right values.
For example: I'm passing an integer 5 into function A, then updates a struct member N within the function with 5. In the main function, after executing function A, I then passed the updated struct member N into function B, but function B doesn't get the value 5, instead i get this value 2293532 when printing the struct member N.
What's wrong with my code:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct _da1
{
int *dynamic;
int array_s;
} da1;
int main()
{
da1 array;
array.dynamic = (int*)realloc(array.dynamic, 0);
array.array_s = 0;
int input;
printf("Input size: ");
scanf("%d",&input);
test_resize(5, array.dynamic, &array.array_s);
printf("%d\n",array.array_s);
test_input(array.dynamic, &array.array_s);
test_print(array.dynamic, &array.array_s);
return 0;
}
void debug(char c)
{
printf("debug %c\n", c);
}
void test_resize(int x, int *a, int s)
{
debug('r');
a = realloc(a, x * sizeof(a));
s = x;
}
void test_input(int *a, int s)
{
debug('i');
int i = 0;
printf("%d \n", s);
for(i = 0; i < s ; i++)
{
printf("input in %d: \n", i);
scanf("%d", &a[i]);
}
}
void test_print(int *a, int s)
{
debug('p');
int i = 0;
for(i = 0; i < s ; i++)
{
printf("in %d = %d \n", i, a[i]);
}
}