blogger Indonesia

Follow judhyns blog

MANIPULATING ARRAYS USING POINTERS

Introduction

When the pointer is incremented by an increment operator, it is always right incremented. That is, if the pointer points to an integer, the pointer is incremented by 2, and, if it is long, it is incremented by 4.

Program

 #include  void printarr(int a[]); void printdetail(int a[]); void print_usingptr(int a[]); main() {     int a[5];     for(int i = 0;i<5;i++)     {         a[i]=i;     }     print_usingptr(a); } void printarr(int a[]) {     for(int i = 0;i<5;i++)     {         printf("value in array %d\n",a[i]);     } } void printdetail(int a[]) {     for(int i = 0;i<5;i++)     {         printf("value in array %d and address is %8u\n",a[i],&a[i]);     } } void print_usingptr(int a[]) {     int *b;     b=a;     for(int i = 0;i<5;i++)     {         printf("value in array %d and address is %16lu\n",*b,b);         b++;                  // A     } } 

Explanation

  1. This function is similar to the preceding function except for the difference at statement A. In the previous version, b = b+2 is used. Here b++ is used to increment the pointer.

  2. Since the pointer is a pointer to an integer, it is always incremented by 2.

Point to Remember

The increment operator increments the pointer according to the size of the data type.

No comments:

feed

PR Check

activesearchresults

judhyn's blog

 

http://www.judhyn.blogspot.com | |