I have a little C program which takes a number of vectors and their corresponding coefficients. With this information, it calculates the length (modulus) of the vector. Next, the program sorts the array with vectors by its length and then displays all vectors in the right order.
Everything seems to work fine. However, when I compile the code with the -wall and -ansi arguments I receive the following warnings:
|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
The code I use:
#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;
}
Any ideas on how to fix those warnings?
Thanks in advance.