C: warning: excess elements in array initializer
-
I've some warnings while trying to compile program in C language
#include
int codecheck(int col,int col_p,int rows_p,int **H,int x[])
{ int i,j,m,f; //flags
int sum = 0;
int y[col];for (i=0;i0) return 0; else return 1;
}
void ModulationSchemeValues (int &modulationScheme, int &noOfSymbols, double &var, float &d, int &col, float &rate, double &snr)
{
switch (modulationScheme)
{
case 0:
var=1/(2*rate*snr);
noOfSymbols = col;
d=1;
break;
case 1:
var=1/(4*rate*snr);
noOfSymbols = col/2;
d=1/sqrt(2);
break;
case 2:
var=1/(8*rate*snr);
noOfSymbols = col/4;
d=1/sqrt(10);
default:
var=1/(2*rate*snr);
noOfSymbols = col;
d=1;
break;
}
}void InitializeChannelLLR (double &var, int &modulationScheme, int &noOfSymbols, float x[], double noise[], double L_ch[], float &d, int &rows_p, vector> &h, double LLR[])
{int j=0;
int k=0,t;
for(int i=0;iPlease stop posting such large code blocks (and repeating similar error messages). You question in QA brokes the system and lets the Firefox thread trying to show your message use 100 % CPU core load!
rog.c:60:34: error: expected ';', ',' or ')' before '&' token
void ModulationSchemeValues (int &modulationScheme, int &noOfSymbols, double &var, float &d, int &col, float &rate, double &snr)That is a C++ style function using references. C does not know references.
prog.c:281:105: note: (near initialization for 'x')
prog.c:281:107: warning: excess elements in array initializer
int x[col]={0,0,1, ...You are passing more initialisers to the array then specified. Don't use variable based sizes with initialised arrays. Just use
int x[] = {0,0,1,/* ... */ };
or specify the size literally. Note also that such variable based array sizes are not supported by all compilers.
-
Please stop posting such large code blocks (and repeating similar error messages). You question in QA brokes the system and lets the Firefox thread trying to show your message use 100 % CPU core load!
rog.c:60:34: error: expected ';', ',' or ')' before '&' token
void ModulationSchemeValues (int &modulationScheme, int &noOfSymbols, double &var, float &d, int &col, float &rate, double &snr)That is a C++ style function using references. C does not know references.
prog.c:281:105: note: (near initialization for 'x')
prog.c:281:107: warning: excess elements in array initializer
int x[col]={0,0,1, ...You are passing more initialisers to the array then specified. Don't use variable based sizes with initialised arrays. Just use
int x[] = {0,0,1,/* ... */ };
or specify the size literally. Note also that such variable based array sizes are not supported by all compilers.
Jochen Arndt wrote:
C does not know references
:omg: Are you sure? I used references in C when I was in school.
The difficult we do right away... ...the impossible takes slightly longer.
-
Jochen Arndt wrote:
C does not know references
:omg: Are you sure? I used references in C when I was in school.
The difficult we do right away... ...the impossible takes slightly longer.
I must confess that I have some difficulties to reply. I should have explained it in another way because naming something that does not exist for a specific subject is problematic. C++ introduced the concept of a reference. These are mainly used for passing arguments. Then it is usually called pass-by-reference. This concept does not exist for C but the poster used C++ style function declarations using that C++ concept. With C, passing arguments by pointers was (and still is) also called pass-by-reference. But passing by pointer is technically passing by value.
www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references[^]:
In C, Pass-by-reference is simulated by passing the address of a variable (a pointer) and dereferencing that address within the function to read or write the actual variable. This will be referred to as "C style pass-by-reference."
-
I must confess that I have some difficulties to reply. I should have explained it in another way because naming something that does not exist for a specific subject is problematic. C++ introduced the concept of a reference. These are mainly used for passing arguments. Then it is usually called pass-by-reference. This concept does not exist for C but the poster used C++ style function declarations using that C++ concept. With C, passing arguments by pointers was (and still is) also called pass-by-reference. But passing by pointer is technically passing by value.
www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references[^]:
In C, Pass-by-reference is simulated by passing the address of a variable (a pointer) and dereferencing that address within the function to read or write the actual variable. This will be referred to as "C style pass-by-reference."
Jochen Arndt wrote:
But passing by pointer is technically passing by value.
No it is not. The pointer points to a variable, it is not the value, but its address (i.e. a reference to it). Consider:
void PassByValue(int foo)
{
// the value of foo is passed directly and may be manipulated here
int i = foo + 5;
}void PassByPointer(int* pFoo)
{
// the value of foo must first be obtained by dereferencing pFoo
int foo = *pFoo; // get the value into a local variable
int i = foo + 5;
} -
Jochen Arndt wrote:
But passing by pointer is technically passing by value.
No it is not. The pointer points to a variable, it is not the value, but its address (i.e. a reference to it). Consider:
void PassByValue(int foo)
{
// the value of foo is passed directly and may be manipulated here
int i = foo + 5;
}void PassByPointer(int* pFoo)
{
// the value of foo must first be obtained by dereferencing pFoo
int foo = *pFoo; // get the value into a local variable
int i = foo + 5;
}pFoo
is a value of typeint*
. If you changepFoo
inside the function, the value ofpFoo
will be changed (the address) but not the value of the variable it points to. Therefore, it is technically passing by value. Compare this with a reference parameterint& foo
. If you changefoo
, the value of the referenced variable will be changed but not the hidden address. -
pFoo
is a value of typeint*
. If you changepFoo
inside the function, the value ofpFoo
will be changed (the address) but not the value of the variable it points to. Therefore, it is technically passing by value. Compare this with a reference parameterint& foo
. If you changefoo
, the value of the referenced variable will be changed but not the hidden address.Jochen Arndt wrote:
pFoo
is a value of typeint*
No it is a pointer type, not a value type. And a reference type (which does not exist in C) is just a shorthand allowing you to modify a caller's value. But under the covers it behaves just the same as a pointer, thus:
void PassByPointer(int* pFoo)
{
*pFoo += 1; // manipulates whatever pFoo points to
}void PassByReference(int& rFoo)
{
rFoo += 1; // manipulates whatever rFoo refers (i.e. points) to
} -
Jochen Arndt wrote:
pFoo
is a value of typeint*
No it is a pointer type, not a value type. And a reference type (which does not exist in C) is just a shorthand allowing you to modify a caller's value. But under the covers it behaves just the same as a pointer, thus:
void PassByPointer(int* pFoo)
{
*pFoo += 1; // manipulates whatever pFoo points to
}void PassByReference(int& rFoo)
{
rFoo += 1; // manipulates whatever rFoo refers (i.e. points) to
}A value is the content held by a variable. A pointer contains an address which is it's value.
-
A value is the content held by a variable. A pointer contains an address which is it's value.
-
I must confess that I have some difficulties to reply. I should have explained it in another way because naming something that does not exist for a specific subject is problematic. C++ introduced the concept of a reference. These are mainly used for passing arguments. Then it is usually called pass-by-reference. This concept does not exist for C but the poster used C++ style function declarations using that C++ concept. With C, passing arguments by pointers was (and still is) also called pass-by-reference. But passing by pointer is technically passing by value.
www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references[^]:
In C, Pass-by-reference is simulated by passing the address of a variable (a pointer) and dereferencing that address within the function to read or write the actual variable. This will be referred to as "C style pass-by-reference."
I appreciate the clarification. However, in school we used the
&
to indicate a reference in C. As in:int PassByeReference(B& Parameter)
{ // Do something with B }
The difficult we do right away... ...the impossible takes slightly longer.
-
I appreciate the clarification. However, in school we used the
&
to indicate a reference in C. As in:int PassByeReference(B& Parameter)
{ // Do something with B }
The difficult we do right away... ...the impossible takes slightly longer.
-
Standard C does not know about references. Also, strict ANSI C does not know about // style comments, but many modern C compilers do. Possibly you were using a C++ compiler to compile c code?
It was an early version of Borland Turbo C for DOS. Maybe they added the reference feature to the language?
The difficult we do right away... ...the impossible takes slightly longer.
-
It was an early version of Borland Turbo C for DOS. Maybe they added the reference feature to the language?
The difficult we do right away... ...the impossible takes slightly longer.
That seems unlikely. I tried the following here : https://archive.org/details/msdos\_borland\_turbo\_c\_2.01, and got the expected compilation error
#include void f(int &x) { x += 1; }
int main(void)
{
int n = 1;
printf("n = %d\n", n);
f(n);
printf("n = %d\n", n);return 0;
}
According to cppreference.com, references were added as early as 1985, and the compiler used above was 2.0, (c) 1990. Its not impossible that Borland added references before that, but them removed them later. Or added them at a later date when their C++ tools came out.
-
It was an early version of Borland Turbo C for DOS. Maybe they added the reference feature to the language?
The difficult we do right away... ...the impossible takes slightly longer.
I have learned C programming with Turbo C in the late 1980's and can't remember that it supported references. Turbo C was discontinued as stand alone product in 1990 and the C compiler was included with Turbo C++ 1.0. So you probably used that.