Good day Mike,
A good way to think of "const" and "static" is with reference to the object that is being declared.
In C++ especially,
declaration is "assignment of space" where
definition is "establish structure/shape/semantic-rules". The difference between the two concepts becomes important with respect to "static" and "const" modifiers.
Inside a class or structure
definition, using "const" sets rules on whether the data members may be modified, and "static" tells the compiler that the member is class-wide rather than instance-specific.
The code fragment defines a structure called butter100_025_coeffs. Using "static" in the structure definition is rarely what one needs. If it were me, I would build a structure whose contents are never intended for modification (for instance being assigned to flash rather than RAM in an embedded device) by using "static const" in the
declaration. In the code below, the compiler complains about the line in main() that tries to modify the contents of the structure.
Code:
typedef struct
{
float A1;
float A2;
} Coeffs;
static const Coeffs myCoeffs =
{
.A1 = -0.993,
.A2 = 1.044
};
int main(int argc, char** argv)
{
printf("coeff A1 is %.3f, A2 is %.3f\n", myCoeffs.A1, myCoeffs.A2);
myCoeffs.A1 = 33.5;
return 0;
}
Code:
$ g++ -o /tmp/cppe /tmp/cppe.cc
/tmp/cppe.cc: In function ‘int main(int, char**)’:
/tmp/cppe.cc:18:17: error: assignment of member ‘Coeffs::A1’ in read-only object
myCoeffs.A1 = 33.5;
<<<commenting out the assignment and trying again>>>
$ g++ -o /tmp/cppe /tmp/cppe.cc
$ /tmp/cppe
coeff A1 is -0.993, A2 is 1.044
$
The bottom line is that the
declaration of myCoeffs places the data in a non-writeable place.
The topics of definition, declaration, static, const and other stuff could involve weeks of study...