Error C2057: expected constant expression
Error C2466: cannot allocate an array of constant size 0
Error C2133: 'inc': unknown size

Why? Here's the code:

#include <stdio.h>
#include <stdlib.h>
#include <fftw3.h>

static double *rdata = NULL, *idata = NULL;
static fftw_plan rplan, iplan;
static int last_fft_size = 0;

double A[8192];
int n = 8192;

int i,m,k;
char erg[35];
char erg2[35];
double absval;
int is16bitfile = 1;

int main() {
	double cc;
	int v, val, val2;
	char inc[n];
	FILE *infile;
	FILE *file;
	FILE *file2;
	int samplerate = 44100;
	int bandwidth = samplerate / 2;
	int nhalf = n / 2;
	double correction = (double)samplerate / (double)n;

	infile = fopen("theevent-zoom.wav", "r");
	file = fopen("data.dat", "w");
	file2 = fopen("data.raw", "w");
	// skip wav file header
	fgets(inc,37,infile);
	fgets(inc,9,infile);

	// read data and fill "A"-array
	for (v=0;v<n;v++) {
		val = fgetc(infile);
		if (is16bitfile) {
			val2 = fgetc(infile);
			if (val2 > 127) { val2 = val2 - 255; }
			A[v] = 256*val2 + val;
		} else {
			A[v] = val;
		}
		sprintf(erg2, "%d %f\n", v, A[v]);
		fputs(erg2, file2);
	}
	
	// prepare fft with fftw
	rdata = (double *)fftw_malloc(n * sizeof(double));
	idata = (double *)fftw_malloc(n * sizeof(double));
	// create the fftw plan
	rplan = fftw_plan_r2r_1d(n, rdata, idata, FFTW_R2HC, FFTW_FORWARD); 

	// we have no imaginary data, so clear idata
	memset((void *)idata, 0, n * sizeof(double));
	// fill rdata with actual data
	for (i = 0; i < n; i++) { rdata[i] = A[i]; }
	// make fft
	fftw_execute(rplan);
	
	// post-process FFT data: make absolute values, and calculate
	//   real frequency of each power line in the spectrum
	m = 0;
	for (i = 0; i < (n-2); i++) {
		absval = sqrt(idata[i] * idata[i]);
		cc = (double)m * correction;
		sprintf(erg, "%f %f\n", cc, absval);
		fputs(erg, file);
		m++;
	}

	// housekeeping
	fclose(file);
	fclose(file2);
	fclose(infile);
	fftw_destroy_plan(rplan);
	fftw_free(rdata);
	fftw_free(idata);
	return 1;
}

Recommended Answers

All 13 Replies

Hi,

As far as I know, we cant use a variable to declare the size of array.
In your code, you have:

char inc[n];  //where your n is integer type.

If you really want to do it that way, then declare a const of int type.

const int size = 8192;

main()
{
     char inc[size];
}

Error C2057: expected constant expression
Error C2466: cannot allocate an array of constant size 0
Error C2133: 'inc': unknown size

Why? Here's the code:

What lines?

What lines?

If I remember correctly it said 21 for all 3 errors, which corresponds to line 22 in the code I posted.

You cannot use a variable in an array definition.

Thanks for your replies. I declared a constant variable, but I still got errors. I think this has to do with dynamic arrays or something, and malloc has some role to play here.

If that's true, then more detail is required. When asking a question, ALWAYS give enough detail for us to understand the problem. Remember, we can't see your computer, your code, your errors. If you don't explain in detail, we can only guess -- generally a waste of our time, and certainly a waste of yours (at least 12 hours since your last post).

If that's true, then more detail is required. When asking a question, ALWAYS give enough detail for us to understand the problem. Remember, we can't see your computer, your code, your errors. If you don't explain in detail, we can only guess -- generally a waste of our time, and certainly a waste of yours (at least 12 hours since your last post).

Sorry about that, I think I'm at a point where I don't understand enough about the code to be able to explain my problem.

But, I do have another question: Does the fact that this was made for Linux have anything to do with its not compiling on Windows? I really don't know anything about these types of things; I thought C was C wherever you compile it.

The code has been compiled with a compiler that supports variable length arrays, Microsoft's C compiler does not and probably never will support that feature.

You can work around this with a simple #define , like so

#define NAME_OF_YOUR_CHOICE_HERE 8192
/* int n = 8192; */
...
int main() {
...
  char inc[NAME_OF_YOUR_CHOICE_HERE];
...

Sorry about that, I think I'm at a point where I don't understand enough about the code to be able to explain my problem.

But, I do have another question: Does the fact that this was made for Linux have anything to do with its not compiling on Windows? I really don't know anything about these types of things; I thought C was C wherever you compile it.

Yes, C is C -- if you write code in Standard C. If you start using additional capabilities beyond the standard that most compilers add in, all bets are off.

Look for a compiler switch that turns on ANSI Standard to flag anything that you use that's not standard.

The code has been compiled with a compiler that supports variable length arrays, Microsoft's C compiler does not and probably never will support that feature.

You can work around this with a simple #define , like so

#define NAME_OF_YOUR_CHOICE_HERE 8192
/* int n = 8192; */
...
int main() {
...
  char inc[NAME_OF_YOUR_CHOICE_HERE];
...

Awesome! But now I've got some new errors (the include is fftw3.h and I think that was made for Linux too. Could this have anything to do with these errors?):

/out:work.exe
work.obj
work.obj : error LNK2019: unresolved external symbol __imp__fftw_free referenced
in function _main
work.obj : error LNK2019: unresolved external symbol __imp__fftw_destroy_plan
referenced in function _main
work.obj : error LNK2019: unresolved external symbol __imp__fftw_execute
referenced in function _main
work.obj : error LNK2019: unresolved external symbol __imp__fftw_plan_r2r_1d
referenced in function _main
work.obj : error LNK2019: unresolved external symbol __imp__fftw_malloc
referenced in function _main
work.exe : fatal error LNK1120: 5 unresolved externals

Well, in any case, I'm in the middle of installing Cygwin (http://www.cygwin.com), which apparently gives me Linux APIs and allows me to compile them in Windows, and in the list I chose (among others) was FFTW3.The only thing left is to figure out how to use the software!

>> I'm in the middle of installing Cygwin

If you don't yet have an IDE which plays nicely with Cygwin's GCC, then perhaps consider installing Code::Blocks too.

As for gcc and these variable length arrays (VLAs), which are a feature of C99 Standard, you'll probably want to use the -std=c99 compiler switch.

Thank you muchly folks! Solved the problem ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.