Today in this tutorial we will learn about pointer and array which is the most important topic in C language, But I will recommend you to refer array and pointer before going further in this tutorial, so that you could understand the topic easily as discussed below. Now let's go ahead...
Example 1: pointer and array
There is an int array named arr[] = {2, 4, 6, 8, 10}; with initialized values {2, 4, 6, 8, 10}. Let's see how to access the array elements as well as address of each elements using pointer.
#include <stdio.h>
int main
{
int arr[] = {2, 4, 6, 8, 10};
int i, *ptr;
/* Assigning the address of 1st element (also known as base address)
* to a pointer (i.e. Initialization of pointer) */
ptr = &arr[0];
/*Printing array elements and address of each elements using pointer */
for(i=0; i<5; i++)
{
printf("arr[%d] = %d and address of arr[%d] = %p", i, *ptr, ptr);
/* Since, *ptr pointing to the base address (i.e. &arr[0]) therefore,
* we need to increment the pointer so that it points to the next
* elements on each iteration like ptr + 1.*/
ptr++
}
return 0;
}
Note: You can use the format specifiers either %u, %p or %x for printing the address of elements in hex format.
Output
arr[0] = 2 and address of arr[0] = 0x9ff02
arr[1] = 4 and address of arr[1] = 0x9ff04
arr[2] = 6 and address of arr[2] = 0x9ff06
arr[3] = 8 and address of arr[3] = 0x9ff08
arr[4] = 10 and address of arr[4] = 0x9ff0A
Pointers arithmetic:
Memory representation of array elements |
We can add and subtract an integer to/from an address. However, We can get address of next element by incrementing pointers with ++ (i.e. ptr++). as we know that pointer contains the address, so the result of an arithmetic operation performed on the pointer will be a pointer. Have a look at the below example how to find the address of next elements using pointers arithmetic.
int arr[5], i, *ptr;
/* Since we know, array name represent base address (i.e. &arr[0]).
* So you can initialize pointer by simply like this: */
ptr = arr;
/* Now let's see how to perform pointers arithmetic? i.e.
* (addition and subtraction an integer to/from a pointer)
* You can use formula to find the address of next elements*/
Formula:
Pointer + n = value of pointer + sizeof (pointer’s type) * n
For Example:
ptr + 0 = 1000 + 4 * 0 = 1000 //address of 1st element i.e. &arr[0]
ptr + 1 = 1000 + 4 * 1 = 1004 //address of 2nd element i.e. &arr[1]
ptr + 2 = 1000 + 4 * 2 = 1008 //address of 3rd element i.e. &arr[2]
ptr + 3 = 1000 + 4 * 3 = 1012 //address of 4th element i.e. &arr[3]
ptr + 4 = 1000 + 4 * 4 = 1016 //address of 5th element i.e. &arr[4]
/* As you know that, to access elements of an array using pointer
* asterisk * symbol is used with pointer name (i.e. *(ptr+i)) */
*(ptr + 0) == arr[0] //to access the 1st element
*(ptr + 1) == arr[1] //to access the 2nd element
*(ptr + 2) == arr[2] //to access the 3rd element
*(ptr + 3) == arr[3] //to access the 4th element
*(ptr + 4) == arr[4] //to access the 5th element
Note: Incrementing pointers with ++, and decrementing with -- is useful when iterating over each elements in an array of elements.
We can access the memory location of next element by incrementing pointers with ++ (i.e. ptr++). As you can see in above example.
Example 2: Pointer and array
Let's rewrite the above code using pointers arithmetic & expression. This way it would be more better than 1st example1.
#include <stdio.h>
int main
{
int arr[] = {2, 4, 6, 8, 10};
int i, *ptr;
/* Assigning base address to a pointer of arr[] (i.e. Initialization of pointer)
* array name 'arr' represent the base address i.e. arr == &arr[0] */
ptr = arr;
/*Printing array elements and address of each elements using pointers*/
for(i=0; i<5; i++)
{
printf("arr[%d] = %d and address of arr[%d] = %p", i, *(ptr+i), ptr+i);
}
return 0;
}
Output
arr[0] = 2 and address of arr[0] = 0x9ff02
arr[1] = 4 and address of arr[1] = 0x9ff04
arr[2] = 6 and address of arr[2] = 0x9ff06
arr[3] = 8 and address of arr[3] = 0x9ff08
arr[4] = 10 and address of arr[4] = 0x9ff0A
More on this to be updated soon...
--
--
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.