In the example, adapted from a book, I want to fully understand how passing and array by reference really works. From playing with the below code in Code Blocks and some reading here and there, I get that passing by value doesn't change the initial variable's value ( the example_pass_by_value). As for the other two variables whose adresses are passed by reference to the change function, I only understood the example_pass_by_reference case like this : when calling the function like this "change (&example_pass_by_reference), you actually put and "=" sign between the formal parameter "by_reference" ( this being declared as pointer in the change function definition) and the address of "example_pass_by_reference" variable whic is passed to the change function ( ex: by_reference = 0x6644fe, or better by_reference = &example_pass_by_reference); The thing i don't understand is how the "=" is put between the address of array in the calling of function "change(array, ....) and the formal parameter "int arr[]" from definition of the function "change(int arr[], ...)", since int arr[] is an array, not a pointer, to be able to receive the address of variable "array".( like int arr[] = array, or int arr[] = 0x7856ff, for example); Thank you in advance and sorry for the subsequent errors in my code or language.
#include <stdio.h>
#include <string.h>
main()
{
int array[3] = {1,2,3};
int example_pass_by_value = 5;
int example_pass_by_reference = 6;
change(array, example_pass_by_value, &example_pass_by_reference);
printf("Back in main(), the array[1] is now %d.\n", array[1]);
printf("Back in main(), the example_pass_by_value is now %d.\n, unchanged", example_pass_by_value);
printf("Back in main(), the example_pass_by_reference is now %d.\n", example_pass_by_reference);
return(0);
}
/******************************************************************/
change(int arr[], int by_value, int *by_reference)
{
// Changes made to all three variables;
arr[1] = 10;
by_value = 100;
*by_reference = 1000;
return;
}
*operator in C is a dereference operator. The C standard says the type a pointer is derived from is the referenced type (C 2018 6.2.5 20). So stop with the nonsense; any way of referring to an object is a reference. - Eric Postpischil[]notation is misleading. It does not denote an array. See e.g. this - n. 1.8e9-where's-my-share m.xactually passes the address ofxin a hidden way. Regardless, a pointer is a reference, the same way “George Washington” is a reference to George Washington. If it were not a reference, we could not dereference it with the dereference operator. When students refer to passing by reference in C, we are not helping them by saying there is no pass by reference in C… - Eric Postpischil