Skip to main content

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

  1. Start
  2. Declare a structure with two integer variables.
  3. Declare a pointer to the structure.
  4. Declare two integer variables.
  5. Assign the pointer to the address of the structure.
  6. Assign the values to the structure variables using the pointer.
  7. Print the values of the structure variables.
  8. 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.