Linked Questions
12 questions linked to/from Is there const in C?
142 votes
9 answers
125k views
'const int' vs. 'int const' as function parameters in C++ and C [duplicate]
Consider: int testfunc1 (const int a) { return a; } int testfunc2 (int const a) { return a; } Are these two functions the same in every aspect or is there a difference? I'm interested in an ...
112 votes
9 answers
31k views
Why do most C developers use define instead of const? [duplicate]
In many programs, a #define serves the same purpose as a constant. For example. #define FIELD_WIDTH 10 const int fieldWidth = 10; I commonly see the first form preferred over the other, relying on ...
92 votes
6 answers
126k views
Variably modified array at file scope
I want to create a constant static array to be used throughout my Objective-C implementation file, similar to something like this at the top level of my ".m" file: static const int NUM_TYPES ...
49 votes
10 answers
8k views
Shall I prefer constants over defines?
In C, shall I prefer constants over defines? I've reading a lot of code lately, and all of the examples make heavy use of defines.
7 votes
7 answers
15k views
C: Behaviour of the `const` keyword
I've been told that if I'm coding in ANSI C to declare in the order that the variables will be used, assert that pointers are not null and that indices are within bounds, and to initialize just before ...
17 votes
4 answers
11k views
C++11 constexpr function's argument passed in template argument
This used to work some weeks ago: template <typename T, T t> T tfunc() { return t + 10; } template <typename T> constexpr T func(T t) { return tfunc<T, t>()...
3 votes
5 answers
2k views
C complains about passing char** value to function taking char const*const*const but C++ doesn't
I'm having difficulties in understanding why C++ behaves in a more "relaxed" way than C when it comes to interpreting and creating types for the parameters of a function. C does the simplest thing in ...
7 votes
5 answers
393 views
What is the result of the Reference Operator "&" on const variables?
I was asked how can a value of a const variable can be changed. My my obvious answer was "pointers!" but I tried the next piece of code and I'm puzzled... int main() { const int x = 5; int *...
0 votes
3 answers
4k views
Saving string to array and printing it out in C
Hope you all doing great! Here is what I'm trying to do - I would like to create a program which will save a string with up to 50 symbols (or less) to an array and then print out each array symbol. ...
11 votes
2 answers
156 views
Assigning pointers to pointers with or without qualifiers [duplicate]
While this compiles: char* p2c; const char* p2cc = p2c; //fine because lhs pointed type has all the qualifiers of rhs pointed type, this does not: char** p2p2c; const char** p2p2cc = p2p2c; //fail ...
2 votes
1 answer
791 views
C++ compiler error: passing pointers but compiler sees references
The following is the method signature in a class. virtual void evaluate(const double *var, double *obj, double *constr) const = 0; virtual void evaluate(unsigned int numPoints, const double **var, ...
6 votes
3 answers
827 views
Why aren't the argv and envp arguments to execve pointers to const?
Take e.g. execve(2), which according to posix has this prototype [1]: int execve(const char *path, char *const argv[], char *const envp[]); To me, it seems as if int execve(const char *path, const ...