You have two problems. The first is that x.a is not set to a value before it is used. You define x at the start of main:
array x;
That creates x but does not put anything in it. Then you pass x.a to fun in:
fun(x.a,num);
This says to pass the value of x.a to fun. But x.a does not have a value yet, so that is wrong. (By the way, make your code readable. Put spaces after commas and semicolons, the same as in normal English text.)
Then, in fun, you use a in:
a=(int*)realloc(a,(i+1)*sizeof(int));
The first time that is executed, a does not have a proper value because it was passed the value from x.a, which was never set. realloc is for reallocating existing allocations. You have to pass it an initialized pointer value. (That includes NULL; you are allowed to pass it NULL to say you want to perform an initial allocation instead of a reallocation.)
The easiest way to fix these problems is to give x.a an initial value, which you can do by changing the definition of x to:
array x = { NULL };
The second problem is that fun never passes an updated value back to its caller. Your declaration of fun is:
void fun(int *a,int num)
This says that fun accepts a value that is a pointer to an int. When fun is called with:
fun(x.a,num);
only the value of x.a is passed. The actual object x.a is not passed, just its value. So fun has no way to return a new value for x.a. There are two ways to fix this. One is to change fun to accept a pointer to x.a:
void fun(int **a,int num)
and change the main routine to pass the address of x.a:
fun(&x.a,num)
and change every use of a inside fun to *a:
*a=(int*)realloc(*a,(i+1)*sizeof(int));
scanf("%d",(*a+i));
The other way is to return the new pointer as the return value of fun. Change the declaration of fun to:
int *fun(int *a,int num)
and change main to update x.a when fun returns it:
x.a = fun(x.a,num);
and change fun to return the value (do not change a to *a as above) by inserting this statement at the end:
return a;
Once that is done, your program will be largely working, but there are two more issues you ought to address:
- It is wasteful to
realloc repeatedly when you already know the final size you want. You ought to just allocate memory once for the size you want. realloc is for when the size you need changes.
realloc can fail. You should modify the code to handle the case when it fails and returns NULL.
x.ais passed tofunby value, sofungets a copy of the pointer and the original one isn't modified. I don't know how you would fix this in C though since I code in C++. Also,realloc()has to copy the existing block of memory to a new location every single time (the reason it's called realloc), so it's more efficient to allocate all the memory at once instead of repeatedly expanding a block of memory.