C expert programming notes

Source: Internet
Author: User

Symbol overloading in C Language

C language is so concise that it does not want to use too many symbols. Many symbols have different meanings in different places.

This will confuse users. This is the language feature of C and also some design mistakes.

Static
Within the function, it indicates that the value of this variable remains continuous throughout each call;
For a function, it indicates that the function is only visible in this file.

Extern

Used for variables, which are defined elsewhere;
Used for Function Definition, indicating global visibility (redundant)

Void
Used in the parameter list, indicating that the function parameter is null, such as int main (void );
Return Value, indicating that this function returns void, that is, it does not return any value, which is equivalent to the process in Pascal;
Indicates a universal pointer in the pointer declaration.

*
Multiplication operator;
Used before a pointer to indicate indirect reference to the content indicated by the pointer;
Indicates a pointer in the declaration. For example, int * PI indicates a pointer to an integer. char * strcpy (...) indicates that the return value of the function is a pointer.

&
Bitwise AND operator;
Bitwise Operator

()
In Function Definition, parameters in the function form are enclosed, such as int main (INT argc, char ** argv );
Call a function, such as srand ();
Change the Operation Sequence of an expression, such as a * (B + C );
Type conversion, such as (int *) X;
Define macros with parameters, such as # define max (A, B) (a)> (B ))? (A): (B)
Operands that enclose the sizeof operator (if it is a type name), such as sizeof (INT)

 

Essential differences between arrays and pointers

The most common phrase that C programmers hear most often is that 'arrays and pointers are the same. 'Unfortunately, this is a dangerous statement and he is not correct.

The most typical error is

File 1:
Int mango [100];
File 2:
Extern int * mango;

This statement is incorrect and must be declared as extern int mango [];

The meaning of the statement has been described in detail in the previous article about extern.

 

First, we need to distinguish between "address y" and "address y content". This is a relatively subtle difference, because in most programming languages, we use the same symbol to represent two things, the compiler determines its meaning based on the context.

Int X, Y;

X = y;

Here we will discuss this assignment statement in depth. It seems that X and Y are the same, both of which are int variables.

But for the compiler, the two sides of a value assignment statement are different (from the perspective of Assembly)

Left = right;

The left value is the address and the right value is the data, which is essentially different.

So the above X is the address represented by X, and Y is the content in the address represented by Y.

So for the compiler,

X = 2. This is very direct. Put 2 on the address represented by X.

X = Y. You need to read data from the address represented by Y first, and then put it on the address represented by X.

Note that the Left value is clearly known during compilation, because the compiler assigns an address for each variable.

The right value is often known only at runtime.

 

Let's take a closer look at why we mistakenly believe that the two are the same.

Int A [10], * P, * q;
P =;
Q = p + 1;

Q = a + 1;

Int I;

I = A [0];

I = (* );

I = (* P );

In the above code, you can operate the array variable name a like the operation pointer. It seems that A is equivalent to P.

Actually, a actually represents the first address of the array, which is a constant and performs q = a + 1, similar to X = 2 + 1.

P is the pointer variable. After p = A, P indicates that the address contains a, that is, the first address of the array.

Then q = p + 1 needs to read a from the address represented by P and put it in the address represented by Q, similar to X = Y + 1.

Similarly, for the * operation, the content on the address is obtained. * A directly obtains the content on the address, while * P first extracts the address data from the address represented by P, then retrieve the content on the address data.

There is also a difference between the two: Time Difference

For q = a + 1, because a represents a constant, the Q value can be calculated during compilation.

For q = p + 1, p is a variable and the Q value must be known at runtime.

 

There is a fundamental difference between the two. A represents a constant. Some people call it a 'constant pointer ', which cannot be assigned or changed. This is absolutely misleading.

A is not a pointer and cannot be used as the left value. Do you have any idea that 2 is used as the left value?

A is equivalent to & P.

The reason for obfuscation is that the C language design is not standardized, and the same statements have completely different operations.

 

I personally think that the array design is inadequate.

Int I, * P;

For general variables, whether I or P, as the left value represents the address represented by the variable name, as the right value represents the data stored on the address represented by the variable name. in fact, there is no difference between I and P at this level, except that the data stored in P is the address rather than other data.

 

But for int A [];

It is different because a is just the starting position of a data set and a cannot be the left value (the designer thinks this is inappropriate ). at the same time, when a is used as the right value, it cannot obtain the data on the address, because it is only the starting position and you do not know how much to get. therefore, the designer breaks the rules of common variables and treats a as an address constant, that is, when a is used as the right value, the address represented by a is taken directly as the right value, instead of taking the data on the address as the right value like a common variable.

The problem is that, according to the C syntax, the usage is very similar to the pointer, and such design is not rigorous. In fact, if this design is more secure,

Using a [] as a whole, you cannot perform any operations on a independently. To obtain the address, use

However, this should not be in line with the simple aesthetics of the C language... the C language is flexible and simple, but many designs are too casual and not rigorous, so it is difficult to understand because many places do not conform to the logic.

 

Array

This book and many articles that explain arrays and pointers are too complicated. I personally think it is to complicate the simple problem.

I have already explained the relationship between arrays and pointers. The following are some additional points:

1. The book points out that arrays and pointers are the same as function parameters (equivalent). This argument comes from "C programming language, 2nd ed, kernighan & Ritchie"

As follows,

Char my_array [10];
Char * my_ptr;
...
I = strlen (my_array );
J = strlen (my_ptr );

In C language, values are passed as function parameters, but arrays are an exception. For large arrays, passing values is obviously quite inefficient.

Therefore, when you use arrays as function parameters, the compiler generates a pointer pointing to the first address of the array and passes the pointer as a parameter to the function.

In fact, if you want to pass the entire array value into the function, as mentioned above, you only need to encapsulate the array in the structure, but this is not recommended, even when the struct object is treated as a parameter, check whether there is an array in the structure. If there is an array, it is best to take the address as a parameter.

Therefore, when used as a function parameter, the first address of the array will be converted to a pointer as a parameter. This is only for the convenience of the compiler.

In this regard, equivalent and I personally think it is inappropriate.

 

2. The book says "the array name in the expression is the pointer", which is quite dangerous and will confuse some programmers who just understand it again.

Int A [100];

Int * P;

P = a + 1;

The meaning in the book is that in this operation, the compiler first encapsulates a as a pointer and then assigns it to P for processing convenience, so we can regard a as a pointer, this statement is not responsible

The compiler can encapsulate a as a pointer for processing convenience, but array name a is definitely not a pointer. If a is a pointer, it should be assigned a value as the left value, a = P

Actually, it cannot. A is an address constant.

 

3. The [] symbol in the array represents the address offset.

During compilation, a [I] will be rewritten to * (a + I)

There is no forward or backward order for +.

Therefore, you can write I [a] in this way, because it is also rewritten to * (I + a) during compilation)

So although it looks very BT, the effect is the same, which is the true meaning.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.