C.Edelweiss C.Edelweiss - 8 days ago 6
C Question

Sorting 2 lists, restricted commands, malloc and pointers

Basically I have to create a program in which

la
is given a parameter, and then I have some specific commands that have to be used to sort them numbers in la into another array,
lb
.

I suppose my prototype should be something like:

int functioname(la) {
//something here
}


My question is how to use malloc to create the
lb
big enough to fit all the numbers in
la
, I have tried to use something like:

lb = malloc(sizeof(la));


but it doesn't seem to work out.

Thank you!

Answer

When using malloc , you have to carefull it returns a void pointer , so you should type cast it.If your parameter is a pointer to the array , you need to find out the lenght of the array. The function prototype has to be int* not int because it has to send back the pointer to the dinamically alocated memory. My piece of code would be int *a =(int*)malloc(numer-of-elememts*sizeof(int));

Comments