Array and pointers are closely related to each other. Since we know that, the name of an array itself represent the address of first element (i.e. 'arr' is equivalent to &arr[0]). Therefor, we can say that an array name 'arr' is a pointer which is holding the address of 1st element of an array.
Let's understand with a example:
#include <stdio.h>
void main()
{
int *ptr; // declaration of pointer
int arr[] = {10, 20, 30, 40, 50}; // Array declaration
ptr = arr; // both arr and ptr pointing to the same element
/* Printing the 1st elements of array using pointer '*ptr' and array name
* '*arr' since array name itself considered as a pointer*/
printf("Value of *ptr = %d", *ptr);
printf("Value of *arr = %d", *arr);
}
Output:
Value of *ptr = 10
Value of *arr = 10
In the above code, we declare an integer pointer (i.e. '*ptr') and an array of integer type (i.e. 'arr[]'). We assign the base address of 'arr[]' to the 'ptr' by using the statement ptr=arr; it means that both the variables 'arr' and 'ptr' point to the same element, i.e., arr[0]. When we try to print the values of *ptr and *arr, then it comes out to be same. Hence, it is proved that the array name stores the address of the first element of an array. As you can see in the above example.
Note: An array name itself considered as a pointer since array name represent the address of base address (i.e. address of 1st element) as you can see the above example.
Array of pointer:
An array of pointer is a group of pointer variables of same data type. Which means each variable is a pointer type addressing to some other variables. For example: Suppose we create an array of pointer holding 5 integer pointers; then its declaration would look like:
int *ptr[5]; // array of 5 integer pointer.
In the above declaration, we declare an array of pointer named as ptr, and it allocates 5 integer pointers in memory.
Memory representation of array of pointer to integers:
Array of pointers | welcome2protec.com |
The element of an array of a pointer can also be initialized by assigning the address of some other element. Let's understand this case through an example.
int a; // variable declaration.
ptr[2] = &a;
In the above code, we are assigning the address of 'a' variable to the third element of an array 'ptr'.
We can also retrieve the value of 'a' be dereferencing the pointer.
*ptr[2];
Let's have a look at the below example to understand it better way:
Example 1: Array of pointers to integers
Array of pointers can be used to point to an array of data items with each element of the pointer array pointing to an element of the data array. Data items can be accessed either directly in the data array, or indirectly by dereferencing the elements of the pointer array. Array pointers is used when we have situations where we need several pointers.
#include <stdio.h>
const int ARRAY_SIZE = 6;
int main ()
{
/* first, declare and set an array of five integers: */
int arr[] = {2, 4, 6, 8, 10, 12};
/* Next, declare an array of five pointers-to-integers: */
int i, *array_of_pointers[ARRAY_SIZE];
for(i = 0; i < ARRAY_SIZE; i++)
{
/* for indices 0 through 5, set a pointer to
point to a corresponding integer: */
array_of_pointers[i] = &arr[i];
}
for ( i = 0; i < ARRAY_SIZE; i++)
{
/* print the values of the integers pointed to by the pointers: */
printf("arr[%d] = %d\n", i, *array_of_pointers[i] );
}
return 0;
}
Output:
arr[0] = 2
arr[1] = 4
arr[2] = 6
arr[3] = 8
arr[4] = 10
arr[5] = 12
Array of Pointer to Strings:
An array of pointer to strings is an array of character pointers that holds the address of the first character of a string or we can say the base address of a string.
Let's see how to declare and initialize an array of pointers to string.
char *months[] = {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
In the above case, each element of the 'months' array is a string literal, and each string literal would hold the base address of the first character of a string. For example, moths[0] contains the base address of "January", months[1] contains the base address of "February", and so on. It is not guaranteed that all the string literals will be stored in the contiguous memory location, but the characters of a string literal are stored in a contiguous memory location.
Let's understand with a simple example:
#include <stdio.h>
int main()
{
char *months[] = {Jan", "Feb", "Mar", "Apr", "May", "Jun"};
/*Displaying the name of the months*/
for(int i=0; i<6; i++){
printf("%s ", months[i]);
}
return 0;
}
Output:
Jan Feb Mar Apr May Jun
In the above program, we have declared an array of char pointer holding 6 string literals, and the first character of each string is holding the base address of the string.
--
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.