What is the use of a constant union object?
You can still initialize the union as follows: const union un a = { .i = 100 }; then use it in your code.
You can still initialize the union as follows: const union un a = { .i = 100 }; then use it in your code.
If you have a const vector<int> you cannot modify the container, nor can you modify any of the elements in the container. You don’t need a const vector<const int> to achieve those semantics.
I believe the only time “const” is appropriate is when there is a spec that you’re coding against that is more durable than the program you’re writing. For instance, if you’re implementing the HTTP protocol, having a const member for “GET” is appropriate because that will never change, and clients can certainly hard-code that into … Read more
When you’re designing C programs for embedded systems, or special purpose programs that need to refer to the same memory (multi-processor applications sharing memory) then you need constant pointers. For instance, I have a 32 bit MIPs processor that has a little LCD attached to it. I have to write my LCD data to a … Read more
register volatile int T=10; volatile qualifier means that the compiler cannot apply optimizations or reorder access to T, While register is a hint to the compiler that T will be heavily used. If address of T is taken, the hint is simply ignored by the compiler. Note that register is deprecated but still used. Practical … Read more
You need to cast away the const to initialize the fields of a malloc’ed structure: struct deneme *mydeneme = malloc(sizeof(struct deneme)); *(int *)&mydeneme->a = 15; *(int *)&mydeneme->b = 20; Alternately, you can create an initialized version of the struct and memcpy it: struct deneme deneme_init = { 15, 20 }; struct deneme *mydeneme = malloc(sizeof(struct … Read more
Observe how the value 195 corresponds to the ret (return from function) instruction on 8086 compatibles. This definition of main thus behaves as if you defined it as int main() {} when executed. On some platforms, const data is loaded into an executable but not writeable memory region whereas mutable data (i.e. data not qualified … Read more
First of all, you need an array, not a pointer. static const char * const days[] = {“mon”, “tue”, “wed”, “thur”, “fri”, “sat”, “sun”}; Second of all, you can’t initialize that directly inside the class definition. Inside the class definition, leave only this: static const char * const days[]; //declaration Then, in the .cpp file, … Read more
Constants (in a sense) in Python 3.8+ Python 3.8 introduces the typing.Final type qualifier, which is used to indicate that a variable or attribute should not be reassigned, redefined, or overridden. PEP 591 — Adding a final qualifier to typing from typing import Final # Annotate module variables # (with or without an explicit type, … Read more
UPDATE: This question was the subject of my blog on June 10th, 2010. Thanks for the great question! why was the decision made to not force constants to use the static modifier if they are considered static? Suppose constants are considered to be static. There are three possible choices: Make static optional: “const int x…” … Read more