Understand C pointers and array in easy way – 2

C pointers and array

Array is an ordered collection of the same type of elements which are stored in contiguous memory locations and these elements can be accessed using indices of an array.
Like all the primitive data types, pointers can also store the address of an array.
In an array if we check the address of the whole array and the first element of the array then we will see that it is the same.

Let’s take an example

#include <stdio.h>
int main ()
{
int nums[]={1,2,3,4,5};
printf("n &nums = %d",&nums);
printf("n &nums[0] = %d",&nums[0]);
return 0;
}

Output :

&nums = 6422300
&nums[0] = 6422300

Above example shows that the address of the first element of an array and address of the whole array is the same.
If we store the address of any element of an array or address of an array in pointer then we can traverse through the entire array.
Pointer can shift from one element to another of an array by using some arithmetic operators and increment/decrement operators.

We know that array elements are stored in contiguous memory locations and the size of each block is dependent on data types of the array.
When we store the address of an array in pointer we can shift pointer forward using ++ or +1 and we can shift pointer backward using — or -1.

For storing address of an array there is no need to use “& address operator” but if we want to store address of any particular element of an array then we need “& address operator”.

Example of pointers with array in C

#include <stdio.h>
int main ()
{
int nums[]={1,2,3,4,5};

//storeing address of an array
int *ptr = nums;

//Printing the value which stored in the address which is stored in ptr
//ptr is pointing to 0th element of array
printf("n *ptr = %d",*ptr);

/*
Incrementing pointer's value using pre-increment operator
This will make ptr to shift forward and store
address of next element of an array
now pointer will point to index 1 of an array
*/
printf("n *(++ptr) = %d",*(++ptr));

/*
Incrementing ptr by 2 means it will now
ptr will point to index 3 of an array
*/
printf("n *(ptr+2) = %d",*(ptr+2));
return 0;
}




Output :
*ptr = 1
*(++ptr) = 2
*(ptr+2) = 4

CHECKOUT OTHER RELATED TOPICS

CHECKOUT OTHER QUIZZES

PRACTICALS IN C PROGRAM

Programs to understand the basic data types and I/O.
Programs on Operators and Expressions.
Programs on decision statements.
Programs on looping.
Programs on arrays.
Programs on functions.
Programs on structures and unions.
Programs on pointers.
Programs on string manipulations.
Practice Programs
1 – C program for leap year, easy and simple code
2 – Best C program to find sum of all digits
3 – Best C program to check Palindrome Number
4 – Best C program to check prime number
5 – Best C program to print Fibonacci series
6 – Best C program to reverse a number
7 – Best C program to reverse an array
8 – Best C program to find average of array
9 – Best C program to find frequency of array element
10 – Best C program to sort array in ascending order
11 – Best C program to find factorial using recursion
12 – Best C program to find sum of all numbers in array
13 – Best C program to find largest element in array
14 – Best C program to swap two numbers without third variable
15 – C Program to check if a number is even or odd
16 – C program to find Simple interest
17 – Best C program to add two matrices
18 – Best C program to count vowels and consonants in a string
19 – Best C program to find LCM
20 – Best C program to find HCF
21 – Best C program to swap two numbers using pointers
22 – Best C program to add two numbers using pointers
23 – Best C program to print Multiplication Table
24 – Best C program to check Armstrong number
25 – Best C program to print half pyramid of star(*)
26 – Best C program to print half pyramid of numbers
27 – Best C program to print reverse half pyramid of star
28 – Best C program to print reverse half pyramid of number
More coming soon…
We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment