练习的目的是根据某个公式计算复数Z,并创建一个n个这样复数的数组。这是计算Z
的函数facet_wrap(~Year, scales = "free_x")
创建数组的函数:
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[100];
for (i=0; i<n; i++)
{
array[i]=convert(*(a+i),i);
}
return array;
}
但是我一直收到同样的错误信息:&#34;赋值给表达式的数组类型&#34;关于这一行:&#34; new_array = array_function(array,N);&#34;
修改:以下是已修改的代码:
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;
}
答案 0 :(得分:1)
您无法分配给C中的数组。您只能分配给数组元素。
如果要动态更改数组,请声明相应类型的指针,并指定结果malloc
和/或realloc
。
答案 1 :(得分:1)
如果您进行了更改以确保通过将scanf
修饰符添加到'l'
格式说明符而使用%f
读取双打(例如{{ 1}})你已经修复了尝试返回一个静态声明的数组,通过在"%lf"
中声明指向main()
的返回值的指针,并在array_function
中正确分配了数组那么你的代码应该可以正常运行而不会崩溃。此外,array_function
应正确输入M_PI
,以消除整数除法的关注。
您必须确认所有用户输入(对不起所有上限,但如果您在这里没有其他任何知识,请了解相关信息)。这意味着验证double
的返回并检查在适当的位置输入的值的范围。
将这些部分组合在一起,您可以执行以下操作(代码间隔足够大,以便老眼睛可以阅读):
scanf
(注意:你应该#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
#include <math.h>
#define MAXC 100 /* if you need a constant, define one */
double complex convert (double R, int p)
{
double complex Z = 0 + 0 * I; /* space your code so it is readable */
double complex A, B, C; /* (especially for older eyes......) */
A = exp (M_PI / 4.0) + 0 * I;
B = cos (11 * M_PI / 6.0 + 2 * p * M_PI) + 0 * I;
C = I * sin (R * M_PI / 6.0);
Z = A * ((R * B) + C);
return Z;
}
double complex *array_function (double *a, int n)
{
int i;
double complex *array = calloc (MAXC, sizeof *array); /* allocate */
if (!array) { /* validate allocation succeeded */
perror ("calloc-array");
exit (EXIT_FAILURE);
}
for (i = 0; i < n; i++) /* convert your values */
array[i] = convert (a[i], i);
return array; /* return pointer */
}
int main (void)
{
int N, i;
double complex *new_array; /* declare pointer to receive return */
double array[MAXC];
printf ("Enter array length: ");
if (scanf("%d", &N) != 1 || N > MAXC) { /* VALIDATE ALL USER INPUT */
fprintf (stderr, "error: invalid input or out of range.\n");
return 1;
}
for (i=0; i<N; i++) {
printf (" enter array[%2d]: ", i);
if (scanf("%lf", &array[i]) != 1) { /* VALIDATE ALL USER INPUT */
fprintf (stderr, "error: invalid conversion, array[%d].\n", i);
return 1;
}
}
new_array = array_function (array, N); /* call array_function */
printf("\nThe new array is: \n\n");
for (i=0; i<N; i++) /* output results */
printf (" %10.6f + i%f \n", creal(new_array[i]), cimag(new_array[i]));
free (new_array); /* don't forget to free memory you allocate */
return 0;
}
你分配的所有内存
示例使用/输出
free
仔细看看,如果您有其他问题,请告诉我。
答案 2 :(得分:0)
double complex *new_array[100];
声明new_array
是一个包含double complex
的100个指针的数组。那不是你想要的。您只需要一个指向double complex
的指针(它将指向函数提供的数组的第一个元素)。声明是double complex *new_array;
。
但是,在array_function
中,您尝试返回array
,其中array
在double complex array[100];
的函数内定义。该声明在函数内部使用时,声明一个只持续到函数返回的数组。如果返回其地址(或其第一个元素的地址),则指向该地址的指针将无效。
从函数返回新数组的正确方法是动态分配数组,如:
double complex *array = malloc(100 * sizeof *array);
if (!array)
{
fprintf(stderr, "Error, failed to allocate memory.\n");
exit(EXIT_FAILURE);
}
… // Assign values to the array elements.
return array;
然后调用者负责通过将地址传递给free
例程来稍后释放数组。
(要使用malloc
,free
和exit
,请在您的计划中添加#include <stdlib.h>
。)
答案 3 :(得分:0)
如果你想让array_function
创建new_array
的内容,可以将数组指针作为参数发送给函数,然后让函数使用它。您还需要将new_array的定义更改为double complex new_array[100]
即,
void array_function (double *a, double complex array[], int n)
{
int i;
for (i=0; i<n; i++)
{
array[i]=convert(*(a+i),i);
}
}
在main()中:
double complex new_array[100];
...
array_function(array, new_array, N);