Pointer, pointer, pointer ..

Source: Internet
Author: User

Pointer, pointer, pointer ..

1. pointer constants and constant pointers

Int a = 10;
Int * const p1 = &;
Const int * p2 = &;

P1 is a constant that cannot change the address, that is, a pointer constant, but can modify the content it points.

P2 is a pointer to a constant, that is, a constant pointer. The address content it points to cannot be modified, but it can indeed be modified.

2. pointer operation

#include<stdio.h>int main(){int *ip1,*ip2,ivalue;char *cp1,*cp2,cvalue;ip1 = (int*)0x500;ip2 = (int*)0x518;ivalue = ip2 - ip1;cp1 = (char*)0x500;cp2 = (char*)0x518;cvalue = cp2 - cp1;printf ("ivalue=%d  cvalue=%d\n",ivalue,cvalue);return 0;}
Running result: ivalue = 6 cvalue = 24

The value of a pointer is related to its type. The two addresses are also 24 bytes different, and the result of integer pointer Subtraction is 24/4 = 6; the result of character pointer Subtraction is 24/1 = 24;

3. pointers and Arrays

#include<stdio.h>int main(){int a[2][3][4] = {{{101,102,103,104},{111,112,113,114},{121,122,123,124}},{{201,202,203,204},{211,212,213,214},{221,222,223,224}}};printf("%d\t%d\n",**(a[0]+2),sizeof(a));return 0;}
Output result: 121 96

Analysis: For an array a, * a is equivalent to a [0], and * (a + 2) is equivalent to a [2]. Likewise, for multi-dimensional arrays * a [0] is equivalent to a [0] [0], * (a [0] + 2) is equivalent to a [0] [2], ** (a [0] + 2) is equivalent to * a [0] [2] and is equivalent to a [0] [2] [0].
Sizeof (a) calculates the memory space occupied by array a: (2*3*4) * 4 Byte = 96 Byte.

4. pointer array and array pointer

Int * p [N], p represents a one-dimensional array that can store N integer pointer elements. p is a pointer array, which is essentially an array and a constant.

Int (* p) [N], p represents a pointer to a one-dimensional array composed of N integer data. p is an array pointer, essentially a pointer, a variable.


What is the pointer to a pointer?

It is a 2-level pointer .. If we need a function parameter, it is an address .. And the function needs to modify the address.
Then we need to pass 2-level pointers.

What is the difference between a constant pointer and a constant pointer?

1 constant pointer! For example, int B, c; int * const a = & B;
A is a constant pointer that points to the memory of variable B. However, because it is a constant pointer, a cannot be used to point to other variables, such as a = & c; error! You can modify the value pointing to the memory, for example, * a = 20; the initial value must be assigned to me when the BTW constant pointer is declared.

2 pointer to constant! For example, int B, c; int const * a; a = & B; a = & c;
Yes, but the memory it points to cannot be modified. For example, * a = 20; this is illegal! Error!

This is the main difference!

BTW also has a skill to remember their different forms! View the const keyword. The key following it cannot be modified. For example, if int * const a = & B; is followed by a, it means a cannot be modified!
Int const * a = & B; * a indicates * a cannot be modified!

Const int a = 5 is often used in many books or MSDN;
Int B = 6;
Const int * p = & B;
In fact, const int * is the same as int const *, that is, the constant pointer, that is, the data it points to (in this case, int) is a constant, and its own data type is const int *
There is also const int * p = & B; yes, although B is not a constant.
But const int a = 6;
Int * p = &;
An error is reported because it eliminates the const attribute of.
**************************************** ******

* ****** We can summarize ********
1. constants (symbolic constants) are the same as regular pointers and declarations of frequently referenced objects.
Definition Format: const data type constant name = constant value;
Or the Data Type const constant name = constant value;

For example: const int a = 7; or int const a = 7; (symbol constant)
Int B = 5;
Const int * p = & B; or int const * p = & B; (regular pointer)
Const int & m = B; (often referenced)
Const Point pt; or Point const pt; // constant objects cannot be updated.
Regular pointers and references have functional limitations, that is, they cannot use them to change the data (value) of the variables they direct)

2. pointer Constants
Definition Format: Data Type * const pointer constant = constant value;
For example, char ch, * const pch = & ch; (I completed it in one step, you can also separate it)
That is to say, the pointer itself is a constant and cannot be changed, that is, the address it points to is fixed. However, ch can be changed.

************************************

The following are common object descriptions in MSDN:
///////////////////////////////////
Initializing Pointers to const Objects
A pointer to a const object can be initialized with a pointer to an object that is not const, but not vice versa. for example, the following initialization is ...... remaining full text>

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.