BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Pointer and Array in C programming with example

Last Updated: September 24, 2017 by Chaitanya Singh | Filed Under: c-programming

In this guide, we will learn how to work with Pointers and arrays in a C program. I recommend you to refer Array and Pointer tutorials before going though this guide so that it would be easy for you to understand the concept explained here.

A simple example to print the address of array elements

#include <stdio.h>
int main( )
{
   int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ;
   /* for loop to print value and address of each element of array*/
   for ( int i = 0 ; i < 7 ; i++ )
   {
      /* The correct way of displaying the address would be using %p format
       * specifier like this:
       * printf("val[%d]: value is %d and address is %p\n", i, val[i], &val[i]);
       * Just to demonstrate that the array elements are stored in contiguous
       * locations, I m displaying the addresses in integer
       */
      printf("val[%d]: value is %d and address is %d\n", i, val[i], &val[i]);
   }
   return 0;
}

Output:

val[0]: value is 11 and address is 1423453232
val[1]: value is 22 and address is 1423453236
val[2]: value is 33 and address is 1423453240
val[3]: value is 44 and address is 1423453244
val[4]: value is 55 and address is 1423453248
val[5]: value is 66 and address is 1423453252
val[6]: value is 77 and address is 1423453256

Note that there is a difference of 4 bytes between each element because that’s the size of an integer. Which means all the elements are stored in consecutive contiguous memory locations in the memory.(See the diagram below)

Pointer-to-array

In the above example I have used &val[i] to get the address of ith element of the array. We can also use a pointer variable instead of using the ampersand (&) to get the address.

Example – Array and Pointer Example in C

#include <stdio.h>
int main( )
{
   /*Pointer variable*/
   int *p;

   /*Array declaration*/
   int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ;

   /* Assigning the address of val[0] the pointer
    * You can also write like this:
    * p = var;
    * because array name represents the address of the first element
    */
   p = &val[0];

   for ( int i = 0 ; i<7 ; i++ )
   {
      printf("val[%d]: value is %d and address is %p\n", i, *p, p);
      /* Incrementing the pointer so that it points to next element
       * on every increment.
       */
      p++;
   }
   return 0;
}

Output:

val[0]: value is 11 and address is 0x7fff51472c30
val[1]: value is 22 and address is 0x7fff51472c34
val[2]: value is 33 and address is 0x7fff51472c38
val[3]: value is 44 and address is 0x7fff51472c3c
val[4]: value is 55 and address is 0x7fff51472c40
val[5]: value is 66 and address is 0x7fff51472c44
val[6]: value is 77 and address is 0x7fff51472c48

Points to Note:
1) While using pointers with array, the data type of the pointer must match with the data type of the array.
2) You can also use array name to initialize the pointer like this:

p = var;

because the array name alone is equivalent to the base address of the array.

val==&val[0];

3) In the loop the increment operation(p++) is performed on the pointer variable to get the next location (next element’s location), this arithmetic is same for all types of arrays (for all data types double, char, int etc.) even though the bytes consumed by each data type is different.

Pointer logic
You must have understood the logic in above code so now its time to play with few pointer arithmetic and expressions.

if p = &val[0] which means
*p ==val[0]
(p+1) == &val[2]  & *(p+1) == val[2]
(p+2) == &val[3]  & *(p+2) == val[3]
(p+n) == &val[n+1) & *(p+n) == val[n+1]

Using this logic we can rewrite our code in a better way like this:

#include <stdio.h>
int main( )
{
   int *p;
   int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ;
   p = val;
   for ( int i = 0 ; i<7 ; i++ )
   {
      printf("val[%d]: value is %d and address is %p\n", i, *(p+i), (p+i));
   }
   return 0;
}

We don’t need the p++ statement in this program.

❮ PreviousNext ❯

Top Related Articles:

  1. C – Pointer to Pointer (Double Pointer) with example
  2. Passing pointer to a function in C with example
  3. Structure in C programming with examples
  4. Passing array to function in C programming with example
  5. C – switch case statement in C Programming with example

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Rajan says

    July 25, 2015 at 11:59 AM

    Instead of:
    p = &val[0];

    Can i write:
    p = val[0];
    Reason: as for a 2D array val[0] refers to address of 0th element of 2D array, which is a 1D array.

    If my reason is incorrect(which i believe is is), please explain the difference between “&val[0]” and “val[0]”, where val is a 2D array

    Reply
    • anonymous_helper says

      June 19, 2017 at 1:33 PM

      p = &val[0], means you are assigning address of 1st element of array(at 0 position) to variable p, whereas p = val[0] means you are assigning value of 1st element to variable p.
      Hope this helps

      Reply
  2. JCB says

    May 18, 2016 at 3:14 PM

    No malloc for your pointer p ?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap