0、前言
指针数组
与数组指针
这两个概念下午给搞晕了,《c++primer》第四单元课后题有涉及数组指针的函数调用,当时没搞懂 int (*p)[N] = &var
的意思,在菜鸟论坛学基础的的时候仔细研究了一下,做下笔记加以区分。
1、指针数组
实例 -int
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
int var[MAX] = {1, 2, 3};
int *ptr[MAX];
for (int i = 0; i < MAX; i++)
{
ptr[i] = &var[i]; // 赋值为整数的地址
}
for (int i = 0; i < MAX; i++)
{
cout << "Value of var[" << i << "] = ";
cout << *ptr[i] << endl;
}
return 0;
}
|
输出结果为:
1
2
3
| Value of var[0] = 1
Value of var[1] = 2
Value of var[2] = 3
|
实例 -char
char *names[MAX]
这种字符型的指针数组是存储指针的数组,但是在理解字符型指针数组的时候,可以将它理解为一个二维数组。
如 const char *names[4] = {“Zara Ali”,”Hina Ali”,”Nuha Ali”,”Sara Ali”},可以理解为一个 4 行 8 列的数组,可以用 cout << *(names[i] + j)<< endl
取出数组中的每个元素。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #include <iostream>
using namespace std;
const int MAX = 4;
int main ()
{
const char *names[MAX] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali",
};
for (int i = 0; i < MAX; i++)
for (int j = 0; j < 8; j++)
{
cout << "Value of names[" << i << "] = ";
cout << *(names[i] + j)<< endl;
}
return 0;
}
|
输出结果为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| Value of names[0] = Z
Value of names[0] = a
Value of names[0] = r
Value of names[0] = a
Value of names[0] =
Value of names[0] = A
Value of names[0] = l
Value of names[0] = i
Value of names[1] = H
Value of names[1] = i
Value of names[1] = n
Value of names[1] = a
Value of names[1] =
Value of names[1] = A
Value of names[1] = l
Value of names[1] = i
Value of names[2] = N
Value of names[2] = u
Value of names[2] = h
Value of names[2] = a
Value of names[2] =
Value of names[2] = A
Value of names[2] = l
Value of names[2] = i
Value of names[3] = S
Value of names[3] = a
Value of names[3] = r
Value of names[3] = a
Value of names[3] =
Value of names[3] = A
Value of names[3] = l
Value of names[3] = i
|
2、数组指针
C++中优先级顺序是*
小于()
,()
等于[]
,()
和[]
的优先级一样,但是结合顺序是从左到右,所以先是()
里的*
和ptr
结合成为一个指针,然后是(*ptr)
和[]
相结合成为一个数组,最后叫一个指针ptr
指向一个数组,简称数组指针。
实例 -int
1
2
3
4
5
6
7
8
9
| int main()
{
int v[3] = {10, 100, 200};
int (*ptr)[3] = &v;
cout<<*(ptr)[0]<<endl;
}
OUTPUT:
10
|
3、注意点:
初始化部分:
(1)
若有这么一个数组var:
1
| int var[3] = {10, 100, 200};
|
初始化指针:
1
2
3
4
5
6
7
8
| /*一维*/
int *p = var; //int *ptr_1 = &var; int *ptr_1 = &var[0];
int *p_1;
p_1 = &var; //p_1 = &var[0];
/*二维*/
int *pr=&nums[0][0];
|
(2)
对于单个数值来说
1
2
| int num = 110;
int *p = #
|
通用方式-好记:
1
2
3
4
5
6
7
8
| /*数组*/
int *ptr = &var[0];
int *ptr = &nums[0][0];
//或者
int *ptr;
ptr = &var;
/*数*/
int *ptr = #
|
显示地址:
1
| cout << p << p+1 <<endl; //0x7fffffffde00 0x7fffffffde04
|
显示数值:
1
| cout << *p << p[1] <<endl; //10 100
|
传递指针给函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| double sum_to_average(int *ptr,int size);
int main()
{
int var[3]={1,5,34};double result =0;
result =sum_to_average(var,3);
cout<<result<<endl;
}
double sum_to_average(int *ptr,int size)
{
int i,sum=0;double result;
for(i=0;i<size;i++)
{sum += ptr[i];}
result = double(sum)/size;
return result; //返回值类型与函数类型相匹配
}
|
函数数组返回指针
1
2
3
4
5
6
7
8
9
10
11
12
| int * getRandom( );
int main ()
{
int *p;
p = getRandom(); //赋值外部函数生成的数组地址
return 0;
}
int * getRandom( ) // 要生成和返回随机数的函数
{
static int array[10]={1,2,3,4,5,6,7,8,9,10};
return array; //返回数组的首地址
}
|
参考文档