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

57.pointer Specification1, Pass by reference

Specification1:

Accept two numbers and print the sum of them using only the pointers of variables.


Program:

#include<stdio.h>

void main()

{

int a,b,c;

int *p,*q,*r;

clrscr();

p=&a;

q=&b;

r=&c;

printf("Enter two numbers:\n");

scanf("%d%d",p,q);

*r=*p+*q;

printf("Sum %d",*r);

getch();

}


Execution:

Enter two numbers:

50

25

Sum 75


Example explained:

Generally we write &a, &b in scanf () to accept two numbers from the keyboard and store into the variables "a", "b".

Because "p" and "q" are the pointers to "a" and "b", we can use "p", "q" instead of &a, &b


     

  


Specification2:

Accept the length, breadth of a rectangle and print the area, perimeter using their pointers.


Program:

#include<stdio.h>

void main()

{

int l,b,area,peri,*x=&l,*y=&b,*a=&area,*p=&peri;

clrscr();

printf("Enter two sides of rectangle:\n");

scanf("%d%d",x,y);

*a=(*x)*(*y);

*p=2*(*x+*y);

printf("Area %d",*a);

printf("\n Perimeter %d",*p);

getch();

}


Execution:

Enter two sides of rectangle:

5

4

Area 20

Perimeter 18


Specification3:

Accept the price, quantity of a product and print the bill.


Program:

#include<stdio.h>

void main()

{

int qty,*q=&qty;

float price,bill,*p=&price,*b=&bill;

clrscr();

printf("Price:");

scanf("%f",p);

printf("Quantity:");

scanf("%d",q);

*b=(*p)*(*q);

printf("Total bill %f",*b);

getch();

}



Execution:

Price: 25.75

Quantity: 5

Total bill 128.750000


Pass by reference (Address):

It is one of the advantages of pointers.

In general a function is capable to accept any number of arguments but returns a single value.

By using the technique called pass by reference a function can return any number of values to the calling function.

Because local variables belonging one function can't be directly access from another function, we send the addresses of actual arguments to the pointers in called by function (Definition). 

So that pointers in function definition can access actual arguments of calling function using indirect operator value at (*) through which any number of values can be returned to the calling function.


Example:





















Execution:

Enter two sides of rectangle:

12

15

Area 180

Perimeter 54

Example explained:

Here function rectangle() accepts length, breadth from the main and returns area, perimeter.

The values of actual arguments "l", "b" are assigned to the formal arguments "x", "y".

The addresses of actual arguments "&area", "&peri" (6232, 6234) are assigned to the formal arguments *a, *p.

From the called by function using value at (*) operator we can directly access area, peri  

*a (value at a)   is   area

*p (value at p)   is   peri


Specification1:

Accept radios and print the area and circumference of a circle using the function circle()


Program:

#include<stdio.h>

#define PI 3.14

void circle(int,float*,float*);

void main()

{

int rad;

float area,cir;

clrscr();

printf("Enter the radios:");

scanf("%d",&rad);

circle(rad,&area,&cir);

printf("Area %f",area);

printf("\n Circumference %f",cir);

getch();

}

void circle(int r,float *a,float *c)

{

*a=PI*r*r;

*c=2*PI*r;

}


Execution:

Enter the radios: 15

Area  706.5

Circumference 94.2


Example explained:

Here function circle() accepts radios from the main and returns area, circumference

The value of actual argument "rad" is assigned to the formal argument "r"

The addresses of actual arguments "&area", "&cir" are assigned to the formal arguments *a, *c.

From the called by function using value at (*) operator we can directly access area, cir  

*a (value at a)   is   area

*c (value at c)   is   cir


Specification2:

Accept any integer and print the minimum and maximum digits of the number using the function limits()


Program:

#include<stdio.h>

void limits(int,int*,int*);   

void main()

{

int n,min,max;

clrscr();

printf("Enter any integer:");

scanf("%d",&n);

limits(n,&min,&max);

printf("Minimum digit %d",min);

printf("\nMaximum digit %d",max);

getch();

}

void limits(int n,int *mi,int *mx)

{

  for(*mi=*mx=n%10;n!=0;n=n/10)

  {

        if(n%10<*mi)

            *mi=n%10;

        if(n%10>*mx)

            *mx=n%10;

  }

}


Execution:

Enter any integer: 6149

Minimum digit 1

Maximum digit 9







Specification3:

Accept two numbers and print their values by interchanging them using the function swap().


Program:

#include<stdio.h>

void swap(int*,int*);

void main()

{

int a,b;

clrscr();

printf("a=");

scanf("%d",&a);

printf("b=");

scanf("%d",&b);

swap(&a,&b);

printf("Values after interchanging:\n");

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

printf("\nb=%d",b);

getch();   

}

void swap(int *p,int *q)

{

int temp;

temp=*p;

*p=*q;

*q=temp;

}


Execution:

a=56

b=23

Values after interchanging:

a=23

b=56


Pointer arithmetics (scale factor):

Scale factor is the quantity of increment in the value of a pointer if it is incremented by one.

Scale factor of a type is equal to its length.


type Scale factor

char 1

int 2

long 4

float 4

double 8

long double 10

void 0


Example1:

void main()

{

int a,*p=&a;

clrscr();

printf("%u",p);

printf("\n%u",p+1);

printf("\n%u",p+2);

getch();

}

Output:

65494

65496

65498


Example explained:

Because the scale factor of int is 2, the differences among p, p+1 and p+2 are 2


Example2:

void main()

{

float a,*p=&a;

clrscr();

printf("%u",p);

printf("\n%u",p+1);

printf("\n%u",p+2);

getch();

}

Output:

65494

65498

65500


Example explained:

Because the scale factor of float is 4, the differences among p, p+1 and p+2 are 4.


Example3:

#include<stdio.h>

void main()

{

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

int i;

clrscr();

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

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

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

getch();

}


Output:

Values of variables:

10   20   30    40   50

 















Example explained:

Here all the int type of variables are declared in a sequence. The memory allocation of which are allocated in consecutive memory allocations like an array

"p" is the pointer to first variable.

Because scale factor of int type is 2 by incrementing the value of "p" by 1 we can access all the variables.



 

0 comments:

Post a Comment