Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Friday, 11 March 2022

58.Pointer representation to a single dimensional array, Execution, Pointer array

 Pointer representation to a single dimensional array:

Whenever a single dimensional array is declared the compiler allocates the memory allocation of elements in contiguous memory allocations.

The address of base or first element is stored in a pointer with the name of array automatically.

Now the pointer can refer every element of array using pointer arithmetics.


Example:

int x[5]={10,20,30,40,50};

















Note:

x+i can be used instead of &x[i] to read the data from keyboard and store into array.

*(x+i) can be used instead of x[i] to access the elements of array.


Specifcation1:

Accept "n" elements into an array and print the element using pointer arithmetics.


Program:

#include<stdio.h>

void main()

{

int a[50],n,i;

clrscr();

printf("How many elements?");

scanf("%d",&n);

printf("Enter %d elements:\n",n);

for(i=0;i<n;i++)

   scanf("%d",a+i);

printf("The given elements are:\n");

for(i=0;i<n;i++)

   printf("%5d",*(a+i));

getch();

}


Execution:

How many elements? 5

Enter 5 elements:

12   34   56   22   44

The given elements are:

12 34 56 22 44


Example explained:

a+i is used to specify the addresses of elements in scanf()

*(a+i) is used to access the value at specified locations.


Specification2:

Accept "n" elements into an array and print the sum of all the elements using pointer arithmetics.


Program:

#include<stdio.h>

void main()

{

int a[50],n,i,sum;

clrscr();

printf("How many elements?");

scanf("%d",&n);

printf("Enter %d elements:\n",n);

for(i=0;i<n;i++)

   scanf("%d",a+i);

for(sum=0,i=0;i<n;i++)

    sum=sum+*(a+i);

printf("Sum of all the element %d",sum);

getch();

}


Execution:

How many elements? 5

Enter 5 elements:

12   34   56   22   44

Sum of all the elements: 168


Sending the address of an array as an argument (Sending an array as an argument):

While sending a single dimensional array as an argument we specify the name of array as actual argument which is the address of array (Address of base element)

Here the formal argument must be a pointer to store the address of array.

Now the formal argument can refer every element of array using pointer arithmetics.

Example:


 



















Output:

Elements of array:

12 34 56 22 44


Example explained:

Both "a" and "p" are pointers to the same array but from different locations.

We can access every element of array from display() using p[i] or *(p+i).



















Specification1:

Accept "n" elements into an array and reverse the elements of array using the function reverse()


Program:

#include<stdio.h>

void reverse(int*,int);

void main()

{

int a[50],n,i;

clrscr();

printf("How many elements?");

scanf("%d",&n);

printf("Enter %d elements:\n",n);

for(i=0;i<n;i++)

   scanf("%d",&a[i]);

reverse(a,n);

printf("The elements of array are:\n");

for(i=0;i<n;i++)

  printf("%5d",a[i]);

getch();

}

void reverse(int *p,int n)

{

int i,j,temp;

for(i=0,j=n-1;i<j;i++,j--)

   {

       temp=p[i];

       p[i]=p[j];

       p[j]=temp;

   }

}

 

Execution:

How many elements? 5

Enter 5 elements:

12   34   56   22   44

The elements of array are:

44 22 56 34 12







Specification2:

Accept "n" elements into an array, copy the elements onto another array using the function copy() and print the resultant array.


Program:

#include<stdio.h>

void copy(int*,int*,int);

void main()

{

int a[50],b[50],n,i;

clrscr();

printf("How many elements?");

scanf("%d",&n);

printf("Enter %d elements:\n",n);

for(i=0;i<n;i++)

   scanf("%d",&a[i]);

copy(a,b,n);

printf("The resultant array:\n");

for(i=0;i<n;i++)

  printf("%5d",b[i]);

getch();

}

void copy(int *p,int *q,int n)

{

int i;

for(i=0;i<n;i++)

    q[i]=p[i];

}

Execution:

How many elements? 5

Enter 5 elements:

12   34   56   22   44

The resultant array

44 34 56 22 44


Pointer-to-pointer:

Pointer is a special kind of variable which stores the address of another variable.

Pointer-to-pointer is a pointer which stores the address of another pointer.

The type of pointer-to-pointer must be equal to the original type.

Pointer-to-pointer must be declared with the operator (**)

Now the pointer-to-pointer can access the value of original variable using second indirect operator (**)

In the following example

"x" is an int type of variable.

"p" is a pointer to the variable "x"

"q" is a pointer to the pointer "p"


 




















Example:

#include<stdio.h>

void main()

{

int x=125,*p=&x,**q=&p;

clrscr();

printf("x=%d",x);

printf("\nx=%d",*p);

printf("\nx=%d",**q);

getch();

}


Output:

x=125

x=125

x=15


Pointer array:

Array is a set of elements of same type stored in contiguous memory allocations.

Pointer array is a set of pointers of same kind stored in contiguous memory allocations.

The address of base pointer is stored in a pointer-to-pointer with the name of pointer array.

Every element of pointer array is capable to store the address of other memory allocation of similar kind.

Now the pointer-to-pointer is capable to access the memory allocations using pointer arithmetics.


Example:


















#include<stdio.h>

void main()

{

int *p[5];       /* Pointer array with 5 pointers of int type */

int a=10,b=20,c=30,d=40,e=50;

int i;

clrscr();

p[0]=&a;p[1]=&b;p[2]=&c;p[3]=&d;p[4]=&e;

printf("Values of variables:\n");

for(i=0;i<5;i++)

   printf("%5d",*(*(p+i)));

getch();

}


Output:

10 20 30 40 50


0 comments:

Post a Comment