I have written the following code. It initializes an array of size "l," then assigns a value to each element in the array with the function "createArray," and then displays the array with the function "showArray."
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void createArray(double *array, long *l)
{
long i;
for(i=0; i < *l; i++)
array[i]=sin(i/12.0);
}
void showArray(double *array, long l)
{
long i;
for(i=0; i < l; i++)
printf("array[%d] = %f \n", i, array[i]);
}
int main()
{
long i, l = 10;
double *array;
array = malloc(l * sizeof(double));
createArray(array, &l);
showArray(array, l);
exit(0);
}
What I would like to do is make the "array = malloc(l * sizeof(double));" statement inside the function "createArray." Something like this:
Code:
void createArray(double *array, long *l)
{
long i;
array = malloc(*l * sizeof(double));
for(i=0; i < *l; i++)
array[i]=sin(i/12.0);
}
This code runs, but all my numbers are garbage. Any ideas on how I might do this?