Pointers are passed by reference-- are arrays also passed by reference or are they passed by value? :confused:
thankyou
ikkin
Printable View
Pointers are passed by reference-- are arrays also passed by reference or are they passed by value? :confused:
thankyou
ikkin
> Pointers are passed by reference
No, pointers are passed by value, just like everything else in C
> are arrays also passed by reference
Not in the C++ sense of reference
An array decays to a pointer to the first element of the array, and that gets passed by value.
So in C prog, what is passed by reference?
-ikkin
There isn't, you fake it by passing a pointer to whatever it is you think you need a reference to.
Eg, in C++ you would do this
But in C, you have to do thisCode:void foo ( int &*ptr ) {
ptr = new int;
}
...
int *ptr; foo( ptr );
Code:void foo ( int **ptr ) {
*ptr = malloc( sizeof int );
}
...
int *ptr; foo( &ptr );
>So in C prog, what is passed by reference?
Nothing, C doesn't support pass by reference. It only seems like it does because you can simulate pass by reference through the use of pointers.
Thank You!