c语言将数组部分复制_C中的数组–第4部分

c语言将数组部分复制

Read:  Arrays in C – Part 3

In the last tutorial I told you about the memory allocation for arrays. If you are still not comfortable with that topic then I would suggest you to go through that tutorial at least once to understand this tutorial. Today I will tell you about the usage of pointers with arrays. As I said earlier pointer works faster in accessing elements of arrays then by accessing using its subscript. For convenience programmers generally adopt the subscript method but for making an efficient program it is always recommended to make use of pointers.

Note: This tutorial is all about programs, so I will give some good examples to explain the topic. Now I am assuming you are comfortable with the basic C language statements, so I will not explain about them explicitly.

Access Array Elements using Pointer



Output

Arrays in C - Part 4

Explanation

  • First of all I have declared an integer array with name nums. The size of the array is five elements. I have also declared an integer variable x and an integer pointer y.
  • After that I have used two for loops. In the first one I have printed the elements and address of each element of the array.
  • Now after the first for loop I have assigned the address of first array element into an integer pointer y.
  • In the second loop I am displaying the address of array elements and its values by using that integer pointer y.
  • To access all the values inside an array I am incrementing the integer point continuously within loop.

Points to Remember

  • Now with the above program I have again verified that array elements are always stored in contiguous memory locations.
  • With the second for loop, consider carefully that an integer pointer y always points to the next element after incrementing it to 1 (this is one of the core concept of pointers in C).
  • To access all the values we have used y integer pointer with printf() function.

What we have learnt?
With the program we have learnt that we can access the whole array by using pointers if we know the base address or address of first element of array.

Another way to get the base address of an array

Output

Arrays in C - Part 4

Explanation
With the above program we conclude that we can also access the base address of an array by just typing its name.

翻译自: https://www.thecrazyprogrammer.com/2015/01/arrays-in-c-part-4.html

c语言将数组部分复制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值