Just learning about call by reference. It's a basic program:
- I've a null pointer array
A
& array_size initlized to 0 - I've a function
arrayInput(...)
with arguments*A
&*array_size
- Goal of this function is to store size input from user to
array_size
& then allocate that size to the above array.
void arrayInput(int *A, int *size) {
printf("Enter the size of the array: ");
scanf("%d", size);
// allocate size to A
A = (int*)malloc(sizeof(int)*(*size));
}
int main(int argc, char **argv) {
int *A, array_size = 0; // pointer array since I want to allocate the size later
arrayInput(A , &array_size);
printf("%d, %d", array_size, sizeof(A));
return 0;
}
Actual Output:
Enter the size of the array: 5
5, 8
Expected Output:
Enter the size of the array: 5
5, 20
So why isn't it allocating memory to the array?