- Difference between int (*p)[3] and int* p[3]?
- C++ 实现
- C++ 实现
Difference between int (*p)[3] and int* p[3]?
指针存放变量的地址或内存位置。指针是地址的符号表示。它们使程序能够模拟引用调用以及创建和操作动态 数据结构。它在 C/C++ 中的一般声明格式如下:
语法:
数据类型 *var_name;
例子:
int *ptr;在这个例子中,“ptr”是一个指针的变量名,它包含一个整数变量的地址。
本文的重点是区分指针的两种声明,即 int (*p)[3] 和 int *p[3]。
对于 int (*p)[3]:这里的“p”是指针的变量名,可以指向一个由三个整数组成的数组。
下面是一个例子来说明int(*p)[3]的使用:
C++ 实现
// C++ program to illustrate the use输出:
// of int (*p)[3]
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Declaring a pointer to store address
// pointing to an array of size 3
int(*p)[3];
// Define an array of size 3
int a[3] = { 1, 2, 3 };
// Store the base address of the
// array in the pointer variable
p = &a;
// Print the results
for (int i = 0; i < 3; i++) {
cout << *(*(p) + i) << " ";
}
return 0;
}
1 2 3
对于 int *p[3]:这里的“p”是一个大小为 3 的数组,可以存储整数指针。
下面是一个例子来说明int *p[3]的使用:
C++ 实现
// C++ program to illustrate the use输出:
// of int*p[3]
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
// Declare an array of size 3 which
// will store integer pointers
int* p[3];
// Integer variables
int a = 1, b = 2, c = 3;
// Store the address of integer
// variable at each index
p[0] = &a;
p[1] = &b;
p[2] = &c;
// Print the result
for (int i = 0; i < 3; i++) {
cout << *p[i] << " ";
}
return 0;
}
1 2 3
注:本文由VeryToolz翻译自 Difference between int (*p)[3] and int* p[3]? ,非经特殊声明,文中代码和图片版权归原作者 ArifShaikh所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。