Practical-19
Write a program with structure and pointer.
Introduction
In this program, we will learn to write a program with structure and pointer.
Algorithm
- Start
- Declare a structure with two integer variables.
- Declare a pointer to the structure.
- Declare two integer variables.
- Assign the pointer to the address of the structure.
- Assign the values to the structure variables using the pointer.
- Print the values of the structure variables.
- End
Code
Practical-19.c
#include <stdio.h>
void main()
{
//Declare two integer variables
int a = 10;
int b = 20;
//Declare a pointer to integer
int *p;
//Assign the pointer to the address of integer b
p = &b;
//Print results
printf("a = %d, b = %d\n", a, b);
//Increment integer at the address given by pointer p
(*p) += 10;
//Print results
printf("a = %d, b = %d\n", a, b);
}
Output
a = 10, b = 20
a = 10, b = 30
Explanation
In this program, we have declared two integer variables a and b. We have also declared a pointer to integer p. We have assigned the pointer to the address of integer b. We have printed the values of a and b. We have incremented the integer at the address given by pointer p by 10. We have printed the values of a and b again. We can see that the value of b has been incremented by 10, but the value of a remains the same.