Pointer or array?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Pointer or array?

1,694 次查看
roberto_m
Contributor III
In an 8-bit mcu, is better in function arguments use pointer or value?
标签 (1)
0 项奖励
回复
3 回复数

1,125 次查看
bigmac
Specialist III

Hello Roberto,

 

My understanding is that, when a function uses an array name as an argument, the contents of the array are not passed to the function, but a pointer to the first element of the array is automatically passed instead.  Therefore I would suspect that passing an array name, or explicitly passing a pointer should give similar results.

 

Regards,

Mac

 

0 项奖励
回复

1,125 次查看
Lundin
Senior Contributor IV

Correct. There is no way to pass an array by value as a function parameter in the C language.

 

 void func (int* x); /* this is a pointer */

 

void func(int x[]); /* this is a pointer */

 

void func(int x[10]); /* this is a pointer */

 

In all 3 cases above, a pointer is passed on the stack, and nothing else.

0 项奖励
回复

1,125 次查看
kef
Specialist I

Pointer of course. Passing array by value you rise stack space requirements. Also passing array by value slows down execution, because it takes time to copy array from source to the stack.

0 项奖励
回复