The difference between an array pointer and an array of pointers

Source: Internet
Author: User

Array Pointers: A pointer to an array, pointer to array
Array of pointers: array of pointer, which is used to store pointers to arrays where array elements are pointers
Int ( 2) [6] Array pointer: Represents a pointer to array a element representation: (a) [i] (pointer to one-dimensional array, also row pointer)
Int p[6] Pointer array: Indicates that the elements in array A are all intType, that is, the int pointer element represents: (A[i]) orA[i] ([] precedence is higher than )
Code:
#include <iostream>
using namespace Std;
int main ()
{
int a[4] = {1,2,3,4};
int
B[4]; Array of pointers
int c) [4]; Array pointers
c = &a; Let C point to the first address of a
for (int i = 0; i<4; i++)
{
B[i] = &a[i]; The elements of the B array are pointers, that is, the element content is the address
}
cout <<
B[1] << Endl; B[1]=&AMP;A[1], which is the address in b[1] (the address is stored in data 2), B[1] is the element content in &a[1], that is, 2
cout << (
c) [2] << endl;//can be ( c) as the array name, equivalent to a, (c) [2]=a[2], i.e. 3
return 0;
}
Execution Result:
2
3

Note : When you define an array pointer (c) [i], you must give the pointer an address of c=&a, without assigning a direct value ((c) [I]=a[i]) to the pointing (address), otherwise an error occurs
Tip : You can use an array pointer (*C) as an array name

Array pointers
Int (p) [6] () has a high precedence, stating that P is a pointer to a one-dimensional array of type int, that the length of the array is n, that is, the step of P is N, and the p+1 (p is going to span the length of n integer data)
int a[3][4];
Int (
p) [4]; Defines an array pointer to an array with 4 elements
P=a; Assigns the first address of the two-dimensional array A to P, i.e. a[0] or &a[0][0]
p++; After the statement executes, the p=p+1,p crosses line A[0] points to the line a[1]

#include <iostream>
using namespace Std;
int main ()
{
int A[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
Int (p) [4];
p = A;
cout << (
p) [0] << Endl;
p++;
cout << (p) [0] << Endl;
cout <<
((p) +1) << Endl,//((p) +1) equivalent (p) [1], *p at this time an address

}
Execution Result:
1
5
6

pointer array int p[n]
[] High priority, first combined with p to become an array, and then by the int
Description This is an integer pointer array, it consists of n pointers Lexington elements (here to execute p+1 is wrong, so the assignment is wrong: P=a,p is an unpredictable representation, there is only p[0],p[ 1]....p[n-1], they are pointer variables can be used to hold the variable address, so p=a; here P represents the value of the first element of the pointer array, the value of the first address of a
int *p[3]; Represents a one-dimensional array holding three pointer variables, respectively p[0],p[1],p[2]
int a[3][4];
for (i=0;i<3;i++)
P[i]=a[i];

PS: The array pointer is just a pointer variable that occupies the memory space of a pointer, the pointer array is multiple pointer variables, in the form of an array of memory, occupy multiple pointers storage space
An array of pointers to represent the elements of column J of line I in the array:
(P[I]+J) ((p+i) +j) ((P+i)) [j] P[i][j]

The difference between an array pointer and an array of pointers

Related Article

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.