C Arrays and Pointers: - in Java, Pointers Are Easy To Deal With
C Arrays and Pointers: - in Java, Pointers Are Easy To Deal With
return 0;
}
C
Passing arrays to a Function
Pointers with arrays
Pointers with functions
Pass arrays to a function
• Single element of an array can be passed in similar manner as passing
variable to a function.
Passing an entire one-dimensional array to a function
While passing arrays as arguments to the function, only the name of the array is
passed (i.e., starting address of memory area is passed as argument).
Passing Multi-dimensional Arrays
To pass two-dimensionalto Function
array to a function as an argument, starting address of
memory area reserved is passed as in one dimensional array
Pointer to an Array
Declaration of array
int arr[5] = { 1, 2, 3, 4, 5 };
arr will give the base address, which is a constant pointer pointing to the
first element of the array, arr[0].
Hence arr contains the address of arr[0] i.e 1000
Printf(“address of array is =%u \n”,arr);
Printf(“address of array is =%u \n”,&arr[0]);
Pointer to an Array (Examples)
Pointer to an Array (Examples)
Pointer to Multidimensional Array
Pointer to Multidimensional Array
A multidimensional array is of form, a[i][j]
In a[i][j], a will give the base address of this array, even a + 0 + 0 will also
give the base address, that is the address of a[0][0] element.
Pointer to Multidimensional Array
Passing Pointer to a function
Just like any other argument, pointers can also be passed to a function
as an argument.
There are two ways to pass arguments/parameters to function calls –
call by value
call by reference.
Call by values
In call by value, If you change the value of function parameter, it is
changed for the current function only.
It will not change the value of variable inside the caller method such as
main()
Call by value (Example)
call by reference