Look at the code given below, here price,quantity and amount are local to main function we pass them to extend where PRICE,QUANTITY and amount are format argument local to the function. My question is how change in amount which is local to void extend will reflect in main without using pointers.
#include <stdio.h>
void extend(float[], float[], double[]);
int main(void) {
float price[10] = { 10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98 };
float quantity[10] = { 4, 8.5, 6, 8.35, 9, 15.3, 3, 5.4, 2.9, 4.8 };
double amount[10] = { 0 };
extend(price, quantity, amount);
for (int i = 0; i < 10; i ) {
printf("%f ", amount[i]);
}
return 0;
}
void extend(float PRICE[], float QUANTITY[], double amount[]) {
for (int i = 0; i < 10; i ) {
amount[i] = PRICE[i] * QUANTITY[i];
}
}
CodePudding user response:
You are mistaken. Actually the function uses pointers. This function declaration
void extend(float[], float[], double[]);
is adjusted by the compiler to the following declaration
void extend(float *, float *, double *);
From the C Standard (6.7.6.3 Function declarators (including prototypes))
7 A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression.
And when the function is called like
extend(price, quantity, amount);
the array designators price, quantity and amount are implicitly converted to pointers to their first elements. So in fact elements of the arrays are passed by reference indirectly through pointers to first elements. Using the pointer arithmetic the elements of the array amount can be changed within the function
amount[i] = PRICE[i] * QUANTITY[i];
From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)
3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.
Pay attention to that for example this expression amount[i]
is equivalent to *( amount i )
.
From the C Standard (6.5.2.1 Array subscripting)
2 A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1) (E2))). Because of the conversion rules that apply to the binary operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).