Why I can't allocate memory for array xa
this way? Compiler says that xa
is not initialized in main, but I want to initialize array in function fun
. In this function, I want to make the memory for the first element and put it in the array, make the memory for second and put it in the array etc. What should I change in this code?
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *a;
} array;
void fun(int *a,int num)
{
int i;
for(i=0;i<num;i++)
{
a=(int*)realloc(a,(i+1)*sizeof(int));
scanf("%d",(a+i));
}
}
int main(int argc,char *argv[])
{
array x;
int i,num;
scanf("%d",&num); //number of elements in array *a (in struct)
fun(x.a,num);
for(i=0;i<num;i++)
printf("%2d",x.a[i]); //prints elements
}
正如你需要传递的地址 num
有scanf
分配给它,你需要传递的地址xa
(并相应地改变匹配参数的类型,以及使用a
内部的fun
),以从fun
分配给它。
You have two problems. The first is that xa
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 xa
to fun
in:
fun(x.a,num);
This says to pass the value of xa
to fun
. But xa
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 xa
, 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 xa
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 xa
is passed. The actual object xa
is not passed, just its value. So fun
has no way to return a new value for xa
. There are two ways to fix this. One is to change fun
to accept a pointer to xa
:
void fun(int **a,int num)
and change the main
routine to pass the address of xa
:
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 xa
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:
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.