Look at this program:
Code:
void func_value(int a)
{
a += 3;
}
void func_ptr(int *a)
{
*a += 3;
}
int main(void)
{
int a;
func_value(a);
func_ptr(&a);
return 0;
}
And here's the assembly for func_value:
Code:
func_value:
pushl %ebp
movl %esp,%ebp
addl $3,8(%ebp)
.L2:
leave
ret
And for func_ptr:
Code:
func_ptr:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
movl 8(%ebp),%edx
movl (%edx),%ecx
addl $3,%ecx
movl %ecx,(%eax)
.L3:
leave
ret
Do you see how the dereferencing costs more than not dereferencing? It takes time and you have to dereference it every time you use it in the function.
also,you forget that by passing pointers,i will not only save some space,but i will also get the ability to change the value of a variable in another funciton from some other function..this would not be possible without returning some values when we pass variables by value...
No...I didn't forget. I said to follow Emmanuel Delaha guidelines and that was one of them. I was making an argument against using pointers for some mysterious "efficiency" reason that doesn't really exist. For passing large data types like structs it's fine. But you're passing a float? Forget it. And if it's an int then you're not even getting of the benefit...just costing yourself the dereferencing. But if you need the function to be able to modify the value in the calling function then yeah, duh, use a pointer.