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

c语言将数组部分复制

Read: Arrays in C – Part 2

So far I told you about some basic topics of arrays in C like initialization of arrays, accepting elements in array, printing the values from an array and so on. Today I will tell you about one advance use of arrays like passing the values of arrays to a function. It can be done in two ways, call by value and call by reference.

Passing Array Elements to a Function

Call by Value

Lets straight away starts with one program.


Output

Arrays in C - Part 3

Explanation

  • I have declared an integer array nums and inserted 10 elements into it.
  • After that I have started one for loop. Inside the loop I have written one statement i.e. printarr(nums[x]). It will call the function printarr().
  • At first the value of x is 0. Then the first element of nums array will be passed to the printarr() function.
  • That value will be received in the formal argument n of the function. After that it will display the element on the screen with printf() function.
  • Now the control again moves to the main() function. The printarr() function is again called and second element of array nums is passed to it. This process will continue until the loop stops executing. And at last the program will stop.

Call by Reference 

Lets understand it with one program.

Explanation

  • The code of the program is almost similar to the previous one. But in this case we are passing the address of array elements to the printarr() function.
  • Note that I have passed the address of array elements with the help of address of operator i.e. &.
  • The address of the array element is received by an integer pointer variable n.
  • And in last I have printed the value by using printf() function. The output will be same as previous program.

Which one is better?
As you can see both the approaches are giving the same results. However I need to do little modifications in each program. Remember that the second program is better than the first one. Because it is using the pointer. Usage of pointer decreases the execution time of program. This is the reason people use to prefer pointers while using arrays in C.

Read: Arrays in C – Part 2

So far I told you about some basic topics of arrays in C like initialization of arrays, accepting elements in array, printing the values from an array and so on. Today I will tell you about one advance use of arrays like passing the values of arrays to a function. It can be done in two ways, call by value and call by reference .

Passing Array Elements to a Function

Call by Value

Lets straight away starts with one program.


Output

Arrays in C - Part 3

Explanation

  • I have declared an integer array nums and inserted 10 elements into it.
  • After that I have started one for loop. Inside the loop I have written one statement i.e. printarr(nums[x]). It will call the function printarr().
  • At first the value of x is 0. Then the first element of nums array will be passed to the printarr() function.
  • That value will be received in the formal argument n of the function. After that it will display the element on the screen with printf() function.
  • Now the control again moves to the main() function. The printarr() function is again called and second element of array nums is passed to it. This process will continue until the loop stops executing. And at last the program will stop.

Call by Reference 

Lets understand it with one program.

Explanation

  • The code of the program is almost similar to the previous one. But in this case we are passing the address of array elements to the printarr() function.
  • Note that I have passed the address of array elements with the help of address of operator i.e. &.
  • The address of the array element is received by an integer pointer variable n.
  • And in last I have printed the value by using printf() function. The output will be same as previous program.

Which one is better?
As you can see both the approaches are giving the same results. However I need to do little modifications in each program. Remember that the second program is better than the first one. Because it is using the pointer. Usage of pointer decreases the execution time of program. This is the reason people use to prefer pointers while using arrays in C.

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

c语言将数组部分复制

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值