Saturday 31 August 2013

Pointer and arrays | C Programming Language Tutorial pdf



Pointer and arrays :

Pointer can be used with array for efficient programming. The name of an array itself indicates the stating address of an array or address of first element of an array.
That means array name is the pointer to starting address or first elements of the array.
If A is an array then address of first element can be expressed as &A[0] or A.The compiler defines array name as a constant pointer to the first element.

Ex: 
#include<stdio.h>
#include< conio.h>
void disp (int *, int);
main( )
{
int i, a[10],n;
printf (“ enter the size of an array:”);
scanf (“%d”, &n);
printf (“ enter the array elements:”);
for (i=0; i<n; i++)
scanf (“%d”, &a[i]);
disp (a,n);
printf(“ array elements after sorting ” );
for(i=0;i<n;i++)
printf (“%d”, a[i]);
}
void disp(int a[ ], int n)
{
int i,j, temp;
for (i=0; i<n; i++)
for (j=0; j<n; j++)
{
if (a[j] > a[i])
{
temp = a[ j ];
a[ j ] = a[ i ];
a[ i ] = temp;
}
}
}

Output :
enter the size of an array: 5
enter the array elements: 20 30 10 50 40
array elements after sorting: 10 20 30 40 50

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved