Dynamic Allocation with array of Pointers!!!!

it is crashing when allocation memory. This is hard;/
HERE IS THE PROGRAM::::
////////////////////////////////PROGRAM REQUIREMENTS///////////////
Write a program to input the number of readings of floating-point numbers that are to be input from the user (key board). Then input the floating-point values from the user.

Then sort the numbers using an array of pointers to the floating point numbers. Do not alter the order of the original array.

Output the elements of the array sorted in ascending order, in the original order, and in descending order

There is to be no subscript array notation i.e. no square brackets. Function main() may contain only declarations, function calls to your declared functions, and comments (calls to malloc/calloc must be in a function other than main()). You must use three functions in addition to main().
///////////////////////MAIN//////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
//Prototype Declarations
float* getData(int* num);
void printData (float** ptr, float* arr, int num);
void sortData(float **ptr, int num);
float** allocPtr(float* arr, int num);

////////////////////////////////Main//////////////////////////////////////////
int main (void)
{
//decs
float* arr;
float** ptr;
int num;

//statements

arr = getData(&num);

ptr = allocPtr(arr, num);

sortData(ptr, num);

printData (ptr, arr, num);







return 0;
} // main


/* :::::::::::::::::::::::getData:::::::::::::::::::::::::::
Pre; divers[][], difficulty[], scores[][], noD
Post;
*/
float* getData(int* num)
{

//Local Declarations

int i;
float* arr;

//statements

printf("Enter number of floats you wish to input: ");
scanf ("%d", num);

//Allocate memory
arr = (float*) calloc (*num, sizeof(float));
if(arr == NULL)
{printf("Out of memory"); exit(100);}
free(arr);

//input data

printf ("Enter the values: ");
for(i = 0; i < *num; i++)
scanf(" %f", (arr + i));

/*

for(i = 0; i < *num; i++)
printf(" %f", *(arr + i));

scanf("%d", &i);

*/

return arr;

}//getdata

float** allocPtr(float* arr, int num)
{
//decs

int i;

float** ptr;

//statement


ptr = (float**) calloc (num, sizeof(float*));
if(ptr == NULL)
{printf("Out of memory"); exit(100);}


for (i = 0; i < num; i++)
**(ptr + i) = *(arr + i);

free(ptr);


/*
printf("\n\n");
for (i = 0; i < num; i++)
printf(" %f", **(ptr+i));
scanf("%d", &i);
*/

return ptr;
}//alocPtr

void printData (float** ptr, float* arr, int num)
{
//decs
int i;

//statements

printf ("The Array you entered is:\n");
for(i = 0; i < num; i++)
printf("\n\t%.2f\n", *(arr + i));

printf("\n");

printf ("Decending Order:\n");
for(i = 0; i < num; i++)
printf("\n\t%.2f\n", **(ptr + i));

printf("\n");

printf ("Ascending Order:\n");
for(i = num - 1; i <= 0; i--)
printf("\n\t%.2f\n", **(ptr + i));

printf("\n");



return;
}


void sortData(float **ptr, int num)
{
//decs
float *temp;
int i;

//statements
for(i=0;i<(num-1);i++)
{
if(**(ptr + i + 1 ) < **(ptr + i))
{
temp = *(ptr + i);
*(ptr + i) = *(ptr + i + 1);
*(ptr + i + 1) = temp;
}
}

return;

}
Your requirement is for C, not C++?
ya i dont know where else to get help from
closed account (D80DSL3A)
It looks like you are freeing the allocated memory too soon. In your getData() you allocate memory, then do this: free(arr); and then you attempt to write arr:
1
2
3
4
5
//input data

printf ("Enter the values: ");
for(i = 0; i < *num; i++)
scanf(" %f", (arr + i));


There are similar problems elsewhere.
You must not free memory until you are completely done with it (after sorting the values and printing the results).
Since your functions return the pointers you can use the returned values to free the memory at the end of the program.
thanks I figured it out! closed!
Topic archived. No new replies allowed.