Reputation: 1
static void fill_array(int arr[], int count, int num)
{
int i;
for(i=0; i<count; i++)
{
arr[i] = num;
}
}
typedef struct
{
int a[10];
} x;
int main(void) {
x *x1;
fill_array(x1->a, 10, 0);
}
While trying this I am getting Runtime error. Can anybody help on this issue? Thanks in advance.
Upvotes: 0
Views: 53
Reputation: 1957
You have not allocated memory for x1.
int main(void) {
x *x1 = malloc(sizeof(x));
if(0 !=x1)
{
fill_array(x1->a, 10, 0);
free(x1);
}
}
Upvotes: 1
Reputation: 36
Your runtime error should be probably a segmentation fault
I can look your first pointeur x *x1;
Wich is not allocated and you are trying to access to the content of a non-allocated pointeur so ... segfault.
There is 2 solution.
declare your structure with local memory x x1;
or allocate it
Upvotes: -1
Reputation: 234875
You didn't allocate any memory for x
: the behaviour of your program is undefined.
For this example, you could get away with
x x1;
fill_array(x1.a, 10, 0);
i.e. use automatic storage duration for x
. This is because x1.a
effectively decays to a pointer when passed to the function fill_array
.
Upvotes: 1