English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

void codfile(int *a,int n,int codlength)
{
const int memory=n*codlength/8;
int finalCod [memory]={0};
int k=0,fptr=0,cptr=0;
for(int i=0;i {
int temp=a[i];
while(cptr {
if(fptr==8){ fptr=0;
k++;
}
finalCod [k]=finalCod[k] | temp/2;
finalCod[k]>>1;
temp/=2;
cptr++;
fptr++;
}
error in line 4: constant expression required

2006-07-04 16:21:48 · 6 answers · asked by simona 1 in Computers & Internet Programming & Design

I did what alakit013 had suggested it does't have any error but display 2597525452
I wanted to display cod 0 or 1 what is problem?

2006-07-04 19:21:06 · update #1

6 answers

The statement
int finalCod [memory]={0};
is illegal because "memory" is a variable.
You need dynamic memory allocation:
either
int *finalCod = new int[memory];
or
int *finalCod = calloc( memory, 1 );
Do not forget to free the memory: for "new" use "delete", for "calloc" use "free()".

2006-07-04 17:46:18 · answer #1 · answered by alakit013 5 · 0 0

Yeah, just remove const in line 3. The reason why it can't work is because you would be changing the value of memory every time you call the function, so just get rid of const. Or maybe in order for your array to work (int finalCod) with memory, it has to be also constant. So try getting rid of const when declaring memory.

Good Luck

2006-07-04 23:30:18 · answer #2 · answered by demaman 3 · 0 0

The error is in this line:
const int memory=n*codlength/8;

memory is a const type. You cannot decide what goes into a constant type at run time. It has to be pre-decided.

2006-07-04 23:25:09 · answer #3 · answered by vin 3 · 0 0

the problem with ur function is that ur allocating memory statically without knowing exactly how much memory to allocate. this is not possible in c++. try the same thing dynamically using the 'new' operator.for eg int n=new int[];

2006-07-04 23:27:31 · answer #4 · answered by nancy_sparkz 2 · 0 0

the code seems fine to me. chk whether the main program which generates a function call to codfile changes teh value of codlength? is codlength of type const?? u can try doing two things to remove ther error....first is directly initialize teh array to NULL and second u may try removing the const keyword.

2006-07-04 23:58:17 · answer #5 · answered by vek14 3 · 0 0

Yeah, what the heck is {0}?
You mean 0? Or 0th element of some array?

2006-07-04 23:24:40 · answer #6 · answered by Anonymous · 0 0

fedest.com, questions and answers