C language statement, pointer and array learning Summary

Source: Internet
Author: User

Summary of C language statement:
I reviewed the C language this week. Below are some learning experiences.

The first statement is:
For example, extern char * (* hander) (int );
So how should we understand this statement?

A declares that reading is started according to its name and then read in priority order.
B B1. brackets
B2: suffix OPERATOR: including () and [], () represents a function, and [] represents an array.
C prefix operator, * indicates the pointer...
D sometimes contains the const keyword or valatile keyword

Take the preceding example as an example. First, hander is the name and then parentheses (* hander). It indicates a pointer,
Then there is the suffix (* hander) (int A), (int A) which represents the function parameters, so (* hander) (int A) represents
It is the function pointer, and the final char *. Therefore, it indicates the function pointer with the return value char * and the parameter int.

In the future, more complex statements will be analyzed in the order of appeal:
A-> B1-> B2-> C-> d

Sometimes, if the keyword const is contained, the analysis is as follows:

(1) const char *;
(2) char * const B;

Then, (1) indicates that the class content that a points to cannot be modified, and a itself can be modified,
(2) It indicates that bitself cannot be modified, and B points to the class capacity that can be modified.

(2) second, pointers and values are not the same thing, but in most cases, we always treat them as the same.
Generally, they can be equivalent
1. the array name is a pointer under the context of the parameter used as a function.
2. In an expression, the array name is a pointer.
3. Use the subscript of the array as the pointer offset.

Why is the array name used as a pointer when used as a function parameter?
It is because other types of parameter calls of the function are value passing (copy the called function real parameter, the function cannot modify the real parameter, but can only modify the copy of the real parameter ), if this method is used for arrays
In fact, we are only interested in a certain number at a certain time. If we regard the array name as a pointer, point it to the first address of the array,
Other parameter passing methods use value passing, which simplifies the compiler.

Below is one of the situations where array pointers cannot be regarded as one thing:
(1) Now let's look at the data access process when the Declaration is an array.
For example:
Extern char a [I];
Assume that address a is 7000, read address 7000, then add I to address 7000, that is, address 7000 + I, then read the number of the address
(2) What if we declare it as a pointer?
For example, extern int * B;
What is the process of accessing the address (B + I?
Assume that the address of B is 7000.
According to address B, read 7000, then read the number in 7000, suppose it is 4000, then 4000 + I; finally read the number in the address (4000 + I;

Imagine what would happen when we define it as an array and declare it as a pointer? (Actually quite dangerous)
If we define it as a pointer and declare it as an array, we can access it normally, but the access paths are different.
In fact, no matter whether we define it as an array or a pointer, we only need to declare it as a pointer and access it in the second method.
Therefore, we 'd better define why and declare why. At this time, pointers and arrays are not the same thing.
The C language is profound and profound. Please visit it slowly !!!

 

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.