Both pointers and arrays are the essence of the C language. For many C programmers, if you ask such a question: What is the difference between an array and a pointer? Their answer is probably: "Arrays and pointers are not the same thing, what is the difference between them?" Indeed, in very rare cases, arrays and pointers can indeed be "universal", but in most cases Under, arrays and pointers are two completely different C
Language characteristics;
First, let's take a look at the definition of pointers and arrays, and the differences between them;
A pointer represents a memory address of a data store of a certain data type in the C language, for example, a pointer to various integers or a pointer to a structure;
An array represents a pattern in which elements of the same C language data type are stored in contiguous memory;
1.2 difference between array and pointerOk, the above is a simple definition of arrays and pointers. Before explaining the difference between an array and a pointer, let's look at an example first;
We define two files: ac and bc, where
[cpp] view plain copy
1. ac file:
2. int array[4] = {1, 2, 3, 4};
3.
4. bc file:
5. #include
6.
7. extern int *array;
8.
9. int main(void)
10. {
11. int i = 0;
12. for (; i <4; i++) {
13. printf("*array = %d", *array++);
14. }
15. return 0;
16. }
We compiled and ran the sample program above, and it didn't work unexpectedly. Some people may not understand why, and they can't find the cause. In fact, the root cause is that they think arrays and pointers are the same! It defines array as an array of 4 int elements, but declares it as an int pointer in bc. In fact, arrays and pointers can only be considered identical under certain contexts. In general, they are different data types, just like float and int types.
The following step-by-step analysis of the difference between arrays and pointers:
Arrays and pointers essentially represent a piece of memory. Array comparisons are "direct". The array name represents the address of the memory, and the pointer is "subtle". It does not represent any meaningful content. It is only assigned to it. It really means a meaningful memory address. This leads to a difference between a pointer and an array: the timing of the definition is different; the array is already determined at compile time, and the pointer is not really determined until the runtime. Arrays are like living in the emperor's house. In life, they have their own land and wealth (memory), and the pointers need to be compiled, linked, run, and so on to get their own wealth (memory). However, the identity (memory) of an array cannot be changed easily once it is determined. They (memory) will accompany the array for a lifetime; while the pointer has many choices, during which he can choose a different lifestyle, such as a character. A pointer can point to a single character and can also represent multiple characters.
The above difference between arrays and pointers leads to another difference between them: access is different; because the array name directly represents its identity (the address of the array in memory), and the pointer needs to be indirectly to know its identity (by reading Take their saved address), so their access to their own "wealth" is naturally different, the array is direct, and the pointer is accessed indirectly. The following diagrams illustrate how arrays and pointers access their own elements:
2. Again on arrays2.1 When the array and the pointer are the same
Figure 2-1 shows when an array and a pointer are the same:
Figure 2-1 shows that an array is not the same as a pointer except in the case of a function parameter or as an rvalue in an expression.
Figure 2-1 When are the pointers and arrays the same
Figure 2-1 shows that an array is not the same as a pointer except in the case of a function parameter or as an rvalue in an expression.
The C language standard defines several rules for when arrays and pointers are identical:
Ø Rule 1: The name of the array in the expression (unlike the declaration) is treated by the compiler as a pointer to the first element of the array.
Ø Rule 2: The subscript is always the same offset as the pointer.
Ø Rule 3: In the declaration of a function parameter, the array name is treated by the compiler as a pointer to the first element of the array.
In short, the relationship between arrays and pointers is quite a bit like poetry and word relations. They are all literary and have many things in common, but they have their own characteristics in terms of specific expressions. The specific meanings of these rules are described below.
2.1.1 Rule 1
Combining rules 1 and 2, that is, a reference to an array subscript can always be written as "a pointer to the starting address of the array plus the offset". For example, if we declare as follows:
Int a[10], *p, i = 2;
You can access a[i] in any of the following ways:
p = a;p[i];
p = a; *(p+i);
p = a + i; *p;
Remember: pointers and arrays are interchangeable in expressions because their final form in the compiler is pointers, and they can all be subscripted.
2.1.2 Rule 2
The array subscript and pointer are always the same. Why do you want to say this? As you may have heard, when writing a program, the array access should be written as a pointer, as this can improve efficiency. For modern compilers, this statement is generally wrong! Modern compilers automatically optimize the access to arrays for their corresponding pointers plus offsets, so there is no more efficient way to say this. In fact, the reason that the access of the array is rewritten as the pointer plus the offset is because it is the most basic way of working under the system.
2.1.3 Rule 3
The parameters of functions in C language are basically called by "transfer value". Only the array is called "reference". When the array is used as a function parameter, it will be automatically converted by the compiler into a pointer to the first element of the array. This is done automatically by the compiler. The reason for this is actually for system performance, because the memory occupied by the array structure is usually large. If the "transfer value" is called, the memory copy will waste a lot of time and space, which is not worth the loss, so when the array is used as a function parameter, The compiler automatically converts it to a pointer to the first element.
KNM3 Series Moulded Case Circuit Breaker
KNM3 series Moulded Case Circuit Breaker is MCCB , How to select good Molded Case Circuit Breaker suppliers? Korlen electric is your first choice. All moulded Case Circuit Breakers pass the CE.CB.SEMKO.SIRIM etc. Certificates.
Moulded Case Circuit Breaker /MCCB can be used to distribute electric power and protect power equipment against overload and short-current, and can change the circuit and start motor infrequently. The application of Moulded Case Circuit Breaker /MCCB is industrial.
Korlen electric also provide Miniature Circuit Breaker /MCB. Residual Current Circuit Breaker /RCCB. RCBO. Led light and so on .
KNM3 series Molded Case Circuit Breaker,Small Size Molded Case Circuit Breaker,Electrical Molded Case Circuit Breaker,Automatic Molded Case Circuit Breaker
Wenzhou Korlen Electric Appliances Co., Ltd. , https://www.zjmannualmotorstarter.com