A detailed (vi) pointer and array for the C + + language pointer usage

Source: Internet
Author: User

mark the source, welcome reprint!!! knowledge First, Welcome to correct!!!

1. Definition

Pointers and arrays are basically equivalent because of the way that pointers are arithmetic and how arrays are handled within C + +. --"C++primerplus" fifth edition

In most cases, C + + interprets the array name as the address of the first element in the group.

   int arr[10] = {1,2,3,4,5};   int *p = arr;   cout<< "--------------3 ways to get the address of an array-------------------" <<endl;   cout<< "Arr     " <<arr<<endl;   cout<< "&arr[0" <<&arr[0]<<endl;   cout<< "P-       ," <<p<<endl;   cout<< "--------------4 ways to get the value of an array-------------------" <<endl;   cout<< "arr[1"     <<arr[1]<<endl;   cout<< "* (arr+1),   " <<* (arr+1) <<endl;   cout<< "* (p+1),     " <<* (p+1) <<endl;   cout<< "P[1"       <<p[1]<<endl;

 

Summarize:

Arrayname[i]    equivalent to  * (arrayname+i)
2. Pointer arrays and Arrays pointers

This part of the reference Micro-blog: http://www.cnblogs.com/Romi/archive/2012/01/10/2317898.html

Pointer array: array of pointers, which is used to store pointers to arrays, which are arrays of elements that are pointers

Array pointer: A pointer to an array, which is a pointer to the array

Also note the differences in their usage, as illustrated below.

int* a[4] Pointer array

Indicates: Array a[4] is a pointer, the type of the pointer is int*, the pointer to the type is int

element means: *a[i] * (A[i]) is the same, because [] priority is higher than *

int (*a) [4] Array pointer

Indicates: A is the type of the pointer is int*[4], and the pointer is of type int () [4]

Element representation: (*A) [i]

#include <bits/stdc++.h>using namespace Std;int main () {   /***   pointer Array    * * < array pstr[3], the element of the array is the pointer >  three-province pointers: The type of the pointer is int *, the type is int, each pointer in the array points to the first address of three strings,    ***/   char *pstr[3] = {    "socialism",    "with",    "Chinese"   };   cout<< "Pstr[0"  <<pstr[0][0]<<endl;;   cout<< "pstr[1"  <<pstr[1]<<endl;;   cout<< "pstr[2"  <<pstr[2]<<endl;;   /***   Array Pointer    * * < pointer pstr1>  three-province pointer: the type of the pointer is int (*) [20], the type is int () [20], and the pointer is pointing to str1   ***/   Char STR1[20] = "characteristics";   char (*PSTR1) [] = &str1;   b=&str1;   cout<< "*pstr1   " <<*PSTR1<<ENDL;;}

 

Note: The string is equivalent to an array constant, the contents are immutable and can only be rvalue, and if viewed as pointers, constant pointers are also pointer constants

Pointer constant: The pointer is a constant, immutable, point to an address can not be changed, but 0 points to the cell is able to change, the general reading constant point

Constant pointer: The value of a pointer cannot be changed, but itself is a variable, which is read as a pointer to a constant

3. Summary of the first address of the array

Array name is the first address of an array

a) Meaning:

Declares an array: TYPE arrayname[n];

There are two layers of meaning: 1.arrayname is an array, the type is type[n]

2.array is a constant pointer, the type of the pointer is type *, the point is type, the value pointed to is the No. 0 element of the array, note that the address of arrayname and the No. 0 element of the group is different, arrayname is a pointer constant, arrayname++ Is wrong.

b) function

sizeof (Arrayname) Arrayname represents the array itself, resulting in the size of the entire array
*arrayname (equivalent to arrayname[0]) Arrayname represents the pointer, and the value of the No. 0 element of the array is obtained.
Arrayname+i Arrayname represents a pointer to a pointer that is shifted backwards from I to a unit of type

Add: sizeof (object) is measured by the type size of the object itself

A detailed (vi) pointer and array for the C + + language pointer usage

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.