C programming tutorials for Beginners – Relationship Between Arrays and Pointers

Hits: 29

(C Programming Tutorials)

Relationship Between Arrays and Pointers

In this tutorial, you’ll learn about the relationship between arrays and pointers in C programming. You will also learn to access array elements using pointers.

  • C Arrays
  • C Pointers

Relationship Between Arrays and Pointers

An array is a block of sequential data. Let’s write a program to print addresses of array elements.

#include <stdio.h>
int main(){
   int x[4];
   int i;

   for(i = 0; i < 4; ++i) {
      printf("&x[%d] = %pn", i, &x[i]);
   }

   printf("Address of array x: %p", x);

   return 0;
}

Output

&x[0] = 1450734448
&x[1] = 1450734452
&x[2] = 1450734456
&x[3] = 1450734460
Address of array x: 1450734448

There is a difference of 4 bytes between two consecutive elements of array x. It is because the size of int is 4 bytes (on our compiler).

Notice that, the address of &x[0] and x is the same. It’s because the variable name x points to the first element of the array.

Relation between arrays and pointers

From the above example, it is clear that &x[0] is equivalent to x. And, x[0] is equivalent to *x.

Similarly,

  • &x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).
  • &x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2).
  • Basically, &x[i] is equivalent to x+i and x[i] is equivalent to *(x+i).

Example 1: Pointers and Arrays

#include <stdio.h>
int main(){
  int i, x[6], sum = 0;
  printf("Enter 6 numbers: ");
  for(i = 0; i < 6; ++i) {
  // Equivalent to scanf("%d", &x[i]);
      scanf("%d", x+i);

  // Equivalent to sum += x[i]
      sum += *(x+i);
  }
  printf("Sum = %d", sum);
  return 0;
}

When you run the program, the output will be:

Enter 6 numbers:  2
 3
 4
 4
 12
 4
Sum = 29 

Here, we have declared an array x of 6 elements. To access elements of the array, we have used pointers.


In most contexts, array names decay to pointers. In simple words, array names are converted to pointers. That’s the reason why you can use pointers to access elements of arrays. However, you should remember that pointers and arrays are not the same.


Example 2: Arrays and Pointers

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

  // ptr is assigned the address of the third element
  ptr = &x[2]; 

  printf("*ptr = %d n", *ptr);   // 3
  printf("*(ptr+1) = %d n", *(ptr+1)); // 4
  printf("*(ptr-1) = %d", *(ptr-1));  // 2

  return 0;
}

When you run the program, the output will be:

*ptr = 3 
*(ptr+1) = 4 
*(ptr-1) = 2

In this example, &x[2], the address of the third element, is assigned to the ptr pointer. Hence, 3 was displayed when we printed *ptr.

And, printing *(ptr+1) gives us the fourth element. Similarly, printing *(ptr-1) gives us the second element.

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!

 

C++ for Beginners: C++ Pointers to Structure

C++ for Beginners: C++ Call by Reference: Using pointers

Statistics with R for Business Analysts – Linear Regression