我有一个小 C 程序,它需要许多向量及其相应的系数。使用此信息,它可以计算向量的长度(模数)。接下来,程序按向量的长度对数组进行排序,然后以正确的顺序显示所有向量。
一切似乎都很好。但是,当我使用 -wall 和 -ansi 参数编译代码时,会收到以下警告:
|23|warning: ISO C90 forbids variable-size array 'v'
|23|warning: ISO C90 forbids mixed declarations and code
|44|warning: passing argument 1 of 'swap' from incompatible pointer type
|44|warning: passing argument 2 of 'swap' from incompatible pointer type
我使用的代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void swap(double **p, double **q)
{
double *tmp;
tmp = *p;
*p = *q;
*q = tmp;
}
int main()
{
int dim, num;
int i, j;
double **w;
scanf("%d %d", &dim, &num);
w = calloc(num, sizeof(double *));
double v[num];
/* Get vector coefficients for each vector and calculate the length of each vector */
for(i = 0; i < num; i++)
{
w[i] = calloc(dim, sizeof(double));
for(j = 0; j < dim; j++)
{
scanf("%le", &w[i][j]);
v[i] += pow(w[i][j], 2);
}
}
/* Sort vectors by length */
for(i = 0; i < num-1; ++i)
{
for(j = num-1; j > i; --j)
if(v[j-1] > v[j])
{
swap(&w[j-1], &w[j]);
swap(&v[j-1], &v[j]);
}
}
/* Display vectors, including their coefficients, ordered by length */
for(i = 0; i < num; i++)
{
for(j = 0; j < dim; j++)
{
printf("%e", w[i][j]);
if(j != dim)
printf(" ");
}
printf("\n");
}
return 0;
}
关于如何解决这些警告的任何想法?
提前致谢。