指针数组 c ++

介绍 (Introduction)

Today in this tutorial, we are going to understand the concept of the Pointer to Array in C++.

今天,在本教程中,我们将了解C ++中的数组指针的概念。

Before we head directly to the topic, we first would like to guide the readers through the pointer arithmetics in C++. It is going to help understand the concept in a better way.

在直接进入该主题之前,我们首先要引导读者学习C ++中的指针算法。 这将有助于更好地理解该概念。

指针算术 (Pointer Arithmetics)

Basically, pointers are variables that store the address of any other variable of a specific data type.

基本上, 指针是存储特定数据类型的任何其他变量的地址的变量。

Let us go through a simple example to get a clear idea about the pointer arithmetics.

让我们来看一个简单的示例,以清楚地了解指针算法。


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{  int  i = 3, *x; float  j = 1.5, *y; cout<<"Value of i= "<<i;cout<<"\nValue of j= "<<j;  x = &i ;y = &j ;//Original pointer valuesprintf("\nOriginal address in x = %u",x);printf("\nOriginal address in y = %u",y);//Increasing pointer addressesx++ ;y++ ;//After incrementprintf("\nNew address in x = %u",x);printf("\nNew address in y = %u",y);cout<<"\n\nSize of int for this compiler = "<<sizeof(int);cout<<"\nSize of float for this compiler = "<<sizeof(float);return 0;
}

Output:

输出:


Value of i= 3
Value of j= 1.5
Original address in x = 7339516
Original address in y = 7339512
New address in x = 7339520
New address in y = 7339516Size of int for this compiler = 4
Size of float for this compiler = 4

Here,

这里,

  • We first initialize two int and float variables i and j respectively with some values,我们首先用两个值分别初始化两个intfloat变量i和j,
  • Then we store the address of the two variables inside two pointers x and y in lines 11 & 12, and print them out,然后,将这两个变量的地址存储在第1112行的两个指针xy中,并将它们打印出来,
  • After that, we increment both the pointers and see the change in output.之后,我们将两个指针都递增,并看到输出的变化。

Observe the 5th and 6th lines of the output. 7339520 is original value in x plus 4, and 7339516 is original value in y plus 4. This so happens because every time a pointer is incremented it points to the immediately next location of its type.

观察输出的第5行和第6行。 7339520是x加上4的原始值,而7339516是y加上4的原始值。这是因为每次指针递增时,它都指向该类型的下一个位置。

That is why, when the integer pointer x is incremented, it points to an address two locations after the current location since an integer is always 4 bytes long (We checked that using the sizeof() function above).

这就是为什么当整数指针x递增时,它指向当前位置后两个位置的地址,因为整数始终长4个字节(我们使用上面的sizeof()函数进行了检查)。

Similarly, y points to an address 4 locations after the current location. This is a very important result and can be effectively used while passing the entire array to a function.

同样, y指向当前位置之后4个位置的地址。 这是非常重要的结果,可以在将整个数组传递给函数时有效地使用

Note: Do not attempt the following operations on pointers, they would never work,

注意:请勿尝试对指针执行以下操作,否则它们将永远无法工作,

  • Addition of two pointers,两个指针相加,
  • Multiplication of a pointer with a constant,指针与常数相乘,
  • Division of a pointer with a constant.用常数除以指针。

C ++中的指针和数组 (Pointers And Arrays in C++)

Now, let us take a deep look at accessing array elements using pointers.

现在,让我们深入了解如何使用指针访问数组元素。


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{  int arr[4]= { 10, 20, 30, 40 };int *ptr;ptr=&arr[0]; //same as ptr=arr(arr is the base address)//printing address and values of array//elements using pointersfor(int i=0; i<4; i++){printf("\nAddress = %u",(ptr+i));cout<<"\tValue = "<<*(ptr+i);}return 0;
}

Output:

输出:


Address = 7339504       Value = 10
Address = 7339508       Value = 20
Address = 7339512       Value = 30
Address = 7339516       Value = 40

In the code above,

在上面的代码中,

  • At first, we initialize an array of size 4 and a pointer for the same type,首先,我们初始化一个大小为4的数组和一个相同类型的指针,
  • After that, we assign the base address or the address of the 1st element of the array to the pointer(Since, arr and &arr[0] are same), ptr,之后,我们将基地址或数组第1个元素的地址分配给指针(因为arr&arr[0]相同) ptr
  • Then we print out the elements and their corresponding addresses using pointers.然后,我们使用指针打印出元素及其对应的地址。

As we discussed in the previous section, (ptr+i) is the address of the ith element from the array arr. And *(ptr+i) actually refers to the value at the address (ptr+i). Therefore, it gives us the value of the ith element.

如上一节所述, (ptr+i)是数组arr中第ith个元素的地址。 *(ptr+i)实际上是指地址(ptr + i)上的值 。 因此,它为我们提供了第ith个元素的值。

使用指针将数组传递给函数 (Passing An Array to Function Using Pointers)

Let us see how we can pass a whole array to a function with the help of pointers as an example, to get a clear understanding of the topic.

让我们看看如何在指针的帮助下将整个数组传递给函数,以使您对该主题有一个清晰的了解。


#include<iostream>
using namespace std;
void show(int *ptr, int n)
{//printing the whole arrayfor(int i=0; i<n; i++){cout<<"arr["<<i<<"] = "<<*(ptr+i)<<endl;}
}
int main()
{  //Array initialisationint arr[5]= { 12, 45, 86, 73, 87 };show( arr, 5);//function call with base address(arr) and no.of elements(6)return 0;
}

Output:

输出:


arr[0] = 12
arr[1] = 45
arr[2] = 86
arr[3] = 73
arr[4] = 87

Similar to the previous example, here we initialize an array inside the main() and pass the base address arr and the array size as parameters to the show() function we defined before.

与前面的示例类似,这里我们在main()内部初始化一个数组,并将基地址arr和数组大小作为参数传递给我们之前定义的show()函数。

After the call of the show() function, at this point, the pointer ptr stores the base address of the array(arr) and n is the size. Further, we use a for loop to print the whole array using the concept of a pointer to array.

在调用show()函数之后,此时,指针ptr存储array(arr)的基地址, n为大小。 此外,我们使用for循环使用指向数组的指针的概念来打印整个数组。

结论 (Conclusion)

So, in this tutorial, we learned the working as well as the use of the Pointers to Arrays in C++. Practicing the above codes/examples would help understand the topic further.

因此,在本教程中,我们学习了C ++中指向数组指针的工作方式和用法。 练习以上代码/示例将有助于进一步理解该主题。

For any questions, feel free to use the comments below.

如有任何疑问,请随时使用以下评论。

参考资料 (References)

  • Pointers to 2D Arrays in C++ – Journal Dev post,指向C ++中2D数组的指针 – Journal Dev post,
  • Pointers in C & C++ – Journal Dev Post,C&C ++中的指针 – Journal Dev Post,
  • Is an array name a pointer? – Stack Overflow Question.数组名称是指针吗? –堆栈溢出问题。

翻译自: https://www.journaldev.com/37702/pointer-to-array-in-c

指针数组 c ++

指针数组 c ++_了解C ++中的数组指针相关推荐

  1. python中对比数组长度_在Python中检索数组长度的首选方法

    python中对比数组长度 The __len__() is a method on container types. However, python also provides another op ...

  2. alxctools索引超出了数组界限_[译]V8中的数组类型

    译者:蒋海涛 JavaScript 对象可以和任何属性有关联.对象属性的名称可以包含任何字符.有趣的是 JavaScript 引擎可以选择名称为纯数字的属性来进行优化,而这个属性其实就是数组 inde ...

  3. java定义数组长度_在JAVA中定义数组时,可不可以一开始不设定数组的长度?

    定义时当然可以:例如 int[] a; 但是要把它初始化成为一个真正意义上的数组就必须设定长度: int [] a = new int[10]; 这时就只能改变数组里的数值而不能改变它的长度了. Ja ...

  4. java不定义数组长度_在JAVA中定义数组时,可不可以一开始不设定数组的长度?...

    MYYA 定义时当然可以:例如 int[] a;但是要把它初始化成为一个真正意义上的数组就必须设定长度: int [] a = new int[10]; 这时就只能改变数组里的数值而不能改变它的长度了 ...

  5. mongo更新数组字段_更新mongodb中嵌套数组中的几个字段(使用pymongo)

    我正在尝试更新数组内数组中的一些字段 示例文档如下:{ id: 987654321 tweets: [ { text: "RT @947FreshFM: A vigil will be he ...

  6. python 数组参数_在Python中获取数组作为GET查询参数

    I know in php I could just use $_GET['key1']['key2'] to retrieve GET data that is sent in the form o ...

  7. c++ 数组引用_在 Solidity中使用值数组以降低 gas 消耗

    背景 我们Datona Labs在开发和测试Solidity数据访问合约(S-DAC:Smart-Data-Access-Contract)模板过程中,经常需要使用只有很小数值的小数组(数组元素个数少 ...

  8. 【C 语言】指针间接赋值 ( 直接修改 和 间接修改 指针变量 的值 | 在函数中 间接修改 指针变量 的值 | 在函数中 间接修改 外部变量 的原理 )

    文章目录 一.直接修改 和 间接修改 指针变量 的值 二.在函数中 间接修改 指针变量 的值 三.在函数中 间接修改 外部变量 的原理 一.直接修改 和 间接修改 指针变量 的值 直接修改 指针变量 ...

  9. 函数中参数有数组时注意的小问题(不一定要传递数组长度,不用返回数组,可以在函数中改变数组元素值)

    函数中参数有数组时注意的小问题: 1.不一定要传递数组长度 2.不用返回数组,可以在函数中改变数组元素值 通过下面这个小例子来验证: #include<iostream> #include ...

最新文章

  1. python读取xml数据并显示为表格_用Python解析XML数据,然后用SQL创建一个数据库
  2. java渡劫期(32)----java进阶(ssm整合项目实战----房屋出租系统(渡劫失败))
  3. 邻接表的构建、DFS、BFS搜索
  4. asp.net UpdatePanel 不能局部刷新问题汇总
  5. 作为一个生鲜电商自媒体
  6. PIE SDK波谱运算
  7. 时间戳与全球唯一性标识
  8. 计算机课堂小游戏活跃气氛,适合小学生在课堂上玩的游戏,简单有趣活跃课堂气氛...
  9. JVM-运行时数据区:Java堆(Heap) 内存管理的核心区
  10. 【银行】2016年中国银行信息科技岗 笔试+面试经验汇总。。。。《转》
  11. widows 程序无响应判断,程序假死状态
  12. win7取消计算机密码怎么设置,win7开机密码怎么取消
  13. mysql5.7优化
  14. NYOJ 1016 德莱联盟(计算几何 线段相交判定)
  15. 互联网打印机协议IPP分析
  16. java秀恩爱代码_Android表白秀恩爱源码
  17. 初识LightGBM
  18. 2019年东北大学计算机系提档线,东北大学2019年部分省、批次、科类录取分数线发布!...
  19. Mysql之一台服务器上装多个mysql-yellowcong
  20. 拼多多跨境电商Temu势起,国内商家跟还是不跟?

热门文章

  1. 接口的隐式和显式实现
  2. WCF编程系列(六)以编程方式配置终结点
  3. C#参考:Linq 概述
  4. [ocUI日记]UIwindow和UIview
  5. 转:LoadRunner检查点使用小结
  6. 整合TextBox与Label 创建新控件--EFLabelText
  7. Web页面打印及GridView导出到Excel
  8. [转载] Python全栈(1)—— Python如何快速下载库与jupyter notebook 的基本使用
  9. [转载] Python中自定义异常与抛出异常
  10. deepin(debian)下使用Git