#include <stdio.h>void main () {int a[5]={10,20,30,40,50};int *aptr,i;//pointer variable declaration aptr=&a[0];//pointer variable points to variable afor (i= 0;i<5;i++)//referencing elements by array subscript printf ("a[%d]=%d\n", I,a[i]); for (i=0;i<5;i++)//referencing elements by array name printf ("*a (a+%d) =%d\n", i,* (A+i)); for (i=0;i<5;i++)//By pointer variable subscript reference element printf ("aptr[%d]=%d\n", I,aptr[i]); for (aptr=a,i=0;aptr<a+5;aptr++,i++)//By pointer variable offset reference element printf ("* (aptr+%d) =%d\n", i,*aptr);}
in the above program, there are four for loops, where the first for loop accesses the elements of an array using an array subscript, and the second for loop accesses the array elements using the array name. In C, an address can also be added and reduced as a general variable, but the addition of 1 and minus 1 of the pointer represents an element cell, the third for loop is the element that accesses the array with a pointer, and the fourth for loop offsets the pointer and then accesses the content that the pointer points to.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Referencing an array element with a pointer and printing output