C Language Tutorials by Ghulam Murtaza Dahar - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Output

Enter student's name: Kevin Amla

Enter roll number: 149

Output

Name: Kevin Amla

Roll: 149

Passing structure by reference

The address location of structure variable is passed to function while passing it by reference. If structure is passed by reference, change made in structure variable in function definition reflects in original structure variable in the calling function.

Write a C program to add two distances(feet-inch system) entered by user. To solve this program, make a structure.

Pass two structure variable (containing distance in feet and inch) to add function by reference and display the result in main function without returning it.

#include <stdio.h>

struct distance{

int feet;

float inch;

};

void Add(struct distance d1,struct distance d2, struct distance *d3);

int main()

{

struct distance dist1, dist2, dist3;

printf("First distance\n");

printf("Enter feet: ");

scanf("%d",&dist1.feet);

printf("Enter inch: ");

scanf("%f",&dist1.inch);

printf("Second distance\n");

printf("Enter feet: ");

scanf("%d",&dist2.feet);

printf("Enter inch: ");

scanf("%f",&dist2.inch);

Add(dist1, dist2, &dist3);

/*passing structure variables dist1 and dist2 by value whereas passing structure variable

dist3 by reference */

printf("\nSum of distances = %d\'-%.1f\"",dist3.feet, dist3.inch);

return 0;

}

void Add(struct distance d1,struct distance d2, struct distance *d3)

{

/* Adding distances d1 and d2 and storing it in d3 */

d3->feet=d1.feet+d2.feet;

d3->inch=d1.inch+d2.inch;

if (d3->inch>=12) { /* if inch is greater or equal to 12, converting it to feet.

*/

d3->inch-=12;

++d3->feet;

}

}