C.Edelweiss C.Edelweiss - 1 year ago 59
C Question

Sorting 2 lists, restricted commands, malloc and pointers

Basically I have to create a program in which

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,

I suppose my prototype should be something like:

int functioname(la) {
//something here

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

lb = malloc(sizeof(la));

but it doesn't seem to work out.

Thank you!

Answer Source

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));

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download