Const int * a, int const * a, int * const A, difference, pointer array, array pointer, declaration and definition

Source: Internet
Author: User

From: http://blog.csdn.net/linyaoxin/article/details/3160977

Const int * a, int const * a, int * const A, difference, pointer array, array pointer, declaration and definition


Several confusing concepts are recorded here
I. ====== method declared in C language, help ==============================

(Http://bbs.chinaunix.net/viewthread.php? Tid = 683372 & extra = Page % 3d1)
------------------- English version ----------------
The precedence rule for understanding c Declarations
Step A: declarations are read by starting with the name and then reading in precedence order.
Step B: The precedence, from high to low, is:
Step B .1: parentheses grouping together parts of a declaration
Step B .2: the Postfix operators: parentheses () indicating a function, and square brackets [] indicating an array.
Step B .3: the prefix OPERATOR: The asterisk denoting "pointer ".
Step C: If a const and/or volatile keyword is next to a type specifier (e.g. Int, long, etc.) It
Applies to the type specifier. Otherwise the const and/or volatile keyword applies to
Pointer Asterisk on its immediate left.
------------------ Chinese Version -------------------
Understand the priority rules declared in C Language
A declares that it reads data starting from its name and then reads data in order of priority:
The priority of B is from high to low:
B .1 The Section enclosed in parentheses in the statement
B .2 suffix OPERATOR:
Parentheses () indicate that this is a function, and
Square brackets [] indicate that this is an array.
B .3 prefix OPERATOR: asterisk * indicates "pointing... Pointer ".
C. If the const and (or) Volatile keywords follow the type specifiers (such as int and long), it acts on the type specifiers. In other cases, the const and (or) Volatile keywords act on the pointer asterisk next to its left.
II. ===== use of const ====================================== ======================
(Http://bbs.chinaunix.net/viewthread.php? Tid = 683333 & extra = & page = 1)
Key Issue: const is a modifier. The key is to check where the position of const modifier is.
1. Const int *
Here, const modifies int, while int defines an integer.
Therefore, the object Value pointed to by * A cannot be modified through * a, but you can assign a value to a to point to different objects.
Eg:
Const int * A = 0;
Const int B = 1;
Int c = 1;
A = & B // OK! Extra: you cannot use a to modify the value of B.
A = & C // OK! Extra: although the C itself is not a constant
* A = 2 // erro! Here, the value of the object pointed to by * A cannot be modified, and the final value assigned to the object is C. Therefore, the value of C cannot be modified through *.
2. int * const
Here, const modifies a, and a represents a pointer address.
Therefore, you cannot assign other address values to a, but you can modify the value pointed to by.
This is a bit different from the cont int *.
3. As for the meaning of int const * A and const int * a, they have the same function equivalent.
Supplement:
4. Const int * const
This indicates that the value of the object to which a points and its address cannot be changed.
5. What is the difference between const int A = 10 and INT const A = 10?
There should be no difference. There are differences in Pointer words.
For pointers, you can specify the pointer itself as const, or specify the data referred to by the pointer As const, or both of them as Const.

Supplement to const:
1. The address of a const object can only be assigned a pointer to a const object.
2. A pointer to a const object can be assigned a non-const object address.
3. the pointer to const is often used as a function form parameter to ensure that the actual object passed to the function will not be modified in the function.
4. A constant cannot be modified after definition, so it must be initialized. Non-initialized constant definitions will lead to compilation errors (the above all indicate const problems, so no value assignment is required, which must be assigned in the actual Statement)

Iii. ========================== pointer array, array pointer ========================================
It is also useful for understanding pointer arrays and array pointers, for example:
-------------- Pointer array (first an array )--------------------
Int * P [10]; // pointer array, containing 10 pointer Elements
That is to say, every element is a pointer. First, parse [] to indicate that it is an array, and then * to indicate the pointer. int to indicate the int type pointer, that is, to define a pointer array, containing 10 int type pointer elements.
-------------- Array pointer (first pointer )--------------------
INT (* P) [10]; // array pointer, which can be used to point to an integer array containing 10 elements.
First parse (), the brackets indicate that this is a pointer, and then [] indicates an array, that is, a pointer to an array is defined. Array pointer.
4. ================================== declaration and definition ====================== ==================================
Declaration: Tell the compiler where the variable type is or the features of the function (number of return value parameter types)
Definition: tells the compiler to allocate a bucket to create variables and functions.

There is only one definition of the same variable (why do we need to allocate space in multiple places? memory is very valuable ~)
But it can be declared multiple times.

Variable obfuscation --
Int A; a declaration or definition
Extern int A; only declares
So the Declaration contains definitions, and there is enough information for the compiler to establish a variable declaration is a definition.
To make int A; not to define (do not allocate space to a) Add extern before
(So extern int A = 1; an error occurs -- bb)

The function is clear --
Declaration means that there is no function body definition.
In fact, the function declaration should have been preceded by extern, but it can be omitted because everyone is very lazy. Who wants to type more?

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.