I am curious to know,whether it is possible to assign array of constants at once at runtime using pointers.
for ex.
FIGURE-1
======================
int *x;
x = (int *) malloc(3 * sizeof(int));
*x= {7,8,2};
printf("first element of array x is %d\n",x[0]);
===========================
results in an error.
I know i can overcome this problem if I do:-
FIGURE-2
=======================================
int *x;
int y[]={7,8,2};
x = (int *) malloc(3 * sizeof(int));
x=y;
printf("first element of array x is %d\n",x[0]);
=======================================
works perfectly fine.
I just want to know when the compiler knows that x has been allocated the size for 3 integers, why does it give error in FIGURE-1 ? Is there any other statement which can be used in place for 3rd statement in FIGURE-1?
thanx.
for ex.
FIGURE-1
======================
int *x;
x = (int *) malloc(3 * sizeof(int));
*x= {7,8,2};
printf("first element of array x is %d\n",x[0]);
===========================
results in an error.
I know i can overcome this problem if I do:-
FIGURE-2
=======================================
int *x;
int y[]={7,8,2};
x = (int *) malloc(3 * sizeof(int));
x=y;
printf("first element of array x is %d\n",x[0]);
=======================================
works perfectly fine.
I just want to know when the compiler knows that x has been allocated the size for 3 integers, why does it give error in FIGURE-1 ? Is there any other statement which can be used in place for 3rd statement in FIGURE-1?
thanx.