C & Data Structures
Authors: Deshpande P. S. Kakde O. G.
Published year: 2006
Pages: 63-65/232
Buy this book on amazon.com >>
 < Day Day Up > 


ADDRESS OF EACH ELEMENT IN AN ARRAY

Introduction

Each element of the array has a memory address. The following program prints an array limit value and an array element address.

Program

#include <stdio.h> void printarr(int a[]); main() { int a[5]; for(int i = 0;i<5;i++) { a[i]=i; } printarr(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 %16lu\n",a[i],&a[i]); \ A } }

Explanation

  1. The function printarr prints the value of each element in arr .

  2. The function printdetail prints the value and address of each element as given in statement A. Since each element is of the integer type, the difference between addresses is 2.

  3. Each array element occupies consecutive memory locations.

  4. You can print addresses using place holders %16lu or %p .

Point to Remember

For array elements, consecutive memory locations are allocated.



 < Day Day Up > 
 < Day Day Up > 


ACCESSING AN ARRAY USING POINTERS

Introduction

You can access an array element by using a pointer. For example, if an array stores integers, then you can use a pointer to integer to access array elements.

Program

#include <stdio.h> void printarr(int a[]); void printdetail(int a[]); main() { int a[5]; for(int i = 0;i<5;i++) { a[i]=i; } printdetail(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[]) \ A { int *b; \ B b=a; \ C for(int i = 0;i<5;i++) { printf("value in array %d and address is %16lu\n",*b,b); \ D b=b+2; \E } }

Explanation

  1. The function print_using pointer given at statement A accesses elements of the array using pointers.

  2. Statement B defines variable b as a pointer to an integer.

  3. Statement C assigns the base address of the array to b, thus the array's first location (a[0]) is at 100; then b will get the value 100. Other elements of the array will add 102,104, etc.

  4. Statement D prints two values: *b means the value at the location specified by b , that is, the value at the location 100. The second value is the address itself, that is, the value of b or the address of the first location.

  5. For each iteration, b is incremented by 2 so it will point to the next array location. It is incremented by 2 because each integer occupies 2 bytes. If the array is long then you may increment it by 4.

Points to Remember

  1. Array elements can be accessed using pointers.

  2. The array name is the pointer constant which can be assigned to any pointer variable.



 < Day Day Up > 
 < Day Day Up > 


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 <stdio.h> 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.



 < Day Day Up > 
C & Data Structures
Authors: Deshpande P. S. Kakde O. G.
Published year: 2006
Pages: 63-65/232
Buy this book on amazon.com >>