The goal of the exercise is to calculate a complex number Z according to some formula and create an array of n such complex numbers. Here's the function that calculates Z
double complex convert(double R, int p)
{
double complex Z=0+0*I;
double complex A, B, C;
A=exp(M_PI/4) + 0*I;
B=cos(11*M_PI/6 + 2*p*M_PI) + 0*I;
C=I*sin(R*M_PI/6);
Z=A*((R*B)+C);
return Z;
}
The function that creates the array:
double complex *array_function (double *a, int n)
{
int i;
double complex array[100];
for (i=0; i<n; i++)
{
array[i]=convert(*(a+i),i);
}
return array;
}
And int main:
int main()
{
int N, i;
double complex *new_array[100];
double array[100];
printf("Enter the length of the array = ");
scanf("%d", &N);
for (i=0; i<N; i++)
{
printf("Element number %d is: ", i+1);
scanf("%f", &array[i]);
}
new_array=array_function(array, N); // where I get the error message
printf("The new array is: \n");
for (i=0; i<N; i++)
{
printf("%f + i%f \n", creal(new_array[i]), cimag(new_array[i]));
}
return 0;
}
But I keep getting the same error message: "assignment to expression with array type" in regards to the line: "new_array=array_function(array, N);"
Edit: Here's the edited code:
double complex convert(double R, int p)
{
double complex Z=0+0*I;
double complex A, B, C;
A=exp(M_PI/4) + 0*I;
B=cos(11*M_PI/6 + 2*p*M_PI) + 0*I;
C=I*sin(R*M_PI/6);
Z=A*((R*B)+C);
return Z;
}
double complex *array_function (double *a, int n)
{
int i;
double complex *array = malloc(100 * sizeof *array);
for (i=0; i<n; i++)
{
array[i]=convert(*(a+i),i);
}
return array;
}
int main()
{
int N, i;
double complex *new_array;
double array[100];
printf("Enter the length of the array = ");
scanf("%d", &N);
for (i=0; i<N; i++)
{
printf("Element number %d is: ", i+1);
scanf("%f", &array[i]);
}
new_array=array_function(array, N); // where I get the error message
printf("The new array is: \n");
for (i=0; i<N; i++)
{
printf("%f + i%f \n", creal(new_array[i]), cimag(new_array[i]));
}
return 0;
}
new_arrayis an array, and you can't assign to arrays in C.return array;returns a pointer to local memory that goes out of scope after the return.array_functionthearray[]is an object of automatic storage duration. This ceases to exist when the function returns. If the caller uses that address, the behaviour is undefined. That's a massive bug. Never return addresses of storage with a lifetime ending at the return.