const is the abbreviation for "constant". Unfortunately, because of this, many people think that the value qualified by const is a constant. This is not correct. More accurately, it should be a read-only variable whose value cannot be used at compile time because the compiler does not know its stored content at compile time. Perhaps this keyword should have been replaced by readonly. So what is the use and meaning of this keyword?
The original purpose of const was to replace precompiled instructions, eliminating their shortcomings and inheriting their advantages. Let's see the difference between it and the #define macro.
In C, the const keyword should probably be replaced with readonly.
In this article, we'll explore the syntax of passing parameters to functions using arrays or pointers. What does it actually do behind the scenes? Can it really pass an array or a pointer to a function? What is the connection and difference between these two ways of passing parameters?
In this article, we'll see what happens when an external reference is declared with the wrong but compatible type. There are two cases involved, refer array as pointer and opposite.
Many beginners don't know about the relationship between pointers and arrays. I'll tell you now: there is no relationship between them, they just often wear similar clothes to tease you. A pointer is a pointer. A pointer variable occupies 4 bytes in a 32-bit system and 8 bytes in a 64-bit system. Its value is the address of a particular memory location. A pointer can point to anything, but can you access anything using this pointer variable?
An array is an array, and its size is related to the type and number of its elements; when defining an array, the type and number of its elements must be specified; an array can hold any kind of data, but not functions. Since there is no relationship between them, why do many people often confuse arrays with pointers, and even think that pointers and arrays are the same?
This is related to the mixed C language reference books on the market. Few books explain this topic thoroughly and clearly. Let's go back to the classics, back to the basics, and get the truth from the classic explanations and interpretations of the masters.
A C program, whatever its size, consists of functions and variables. A function contains statements that specify the computing operations to be done, and variables store values used during the computation.
Every time we want to use a variable(e.g. char c; int i;), we must declare it in advance, which actually allocates a space in memory with the width corresponding to the variable type.
A pointer is a variable that contains the address of another variable.
An object doesn't just need enough storage to hold its representation. In addition, on some machine architectures, the bytes used to hold it must have proper alignment for the hardware to access it efficiently.
Where alignment most often becomes visible is in object layouts: sometimes structs contain "holes" to improve alignment.
One of the low-level features of C/C++ is the ability to specify the precise alignment of objects in memory to take maximum advantage of a specific hardware architecture. By default, the compiler aligns class and struct members on their size value.