Programming in C 312303 Practical No 24: Write C Program to print addresses and values of variables using Pointer

Programming in C 312303 Practical No 24: Write C Program to print addresses and values of variables using Pointer

In this Programming in C 312303 Practical No 24, we are going to learn about Writing C Program to print addresses and values of variables using Pointer.

312303 Practical No 24 Write C Program to print addresses
                       312303 Practical No 24 Write C Program to print addresses

Students will be able to understand the concept of pointer variables and how to access their addresses. Students will be able to declare, initialize, and access pointers. After the completion of this practical students will be able to use pointers to get the memory address of a variable.

Exercise

1. Write a C program to access and display the address of variables.

Answer:

#include <stdio.h>

int main() {
  // Declare variables of different data types
  int num = 10;
  char ch = ‘A’;
  float dec_num = 3.14;
  double large_num = 123456.789;

  // Declare pointers for each data type
  int *int_ptr;
  char *char_ptr;
  float *float_ptr;
  double *double_ptr;

  // Assign addresses of variables to their respective pointers
  int_ptr = &num;
  char_ptr = &ch;
  float_ptr = &dec_num;
  double_ptr = &large_num;

  // Print variable values and their addresses
  printf(“Integer num: %d (Address: %p)n”, num, int_ptr);
  printf(“Character ch: %c (Address: %p)n”, ch, char_ptr);
  printf(“Float dec_num: %.2f (Address: %p)n”, dec_num, float_ptr);
  printf(“Double large_num: %.2lf (Address: %p)n”, large_num, double_ptr);

  return 0;
}

Output:

Integer num: 10 (Address: 0x7fff80293bdc)
Character ch: A (Address: 0x7fff80293bdb)
Float dec_num: 3.14 (Address: 0x7fff80293bd4)
Double large_num: 123456.79 (Address: 0x7fff80293bc8)
=== Code Execution Successful ===


Algorithm

  • Include stdio.h header for input/output functions.
  • Declare variables of different data types (int, char, float, double) and initialize them with values.
  • Declare pointers of the same data types as the variables.
  • Assign the addresses of the variables to their respective pointers using the address-of operator (&).
  • Print the value and address of each variable using printf:
  • Use appropriate format specifiers for different data types (%d for int, %c for char, %.2f for float, %.2lf for double).
  • Use %p to print the address stored in the pointer.

Flow Chart

        +———+
          | Start   |
          +———+
                 |
                 v
          +———+         +———+
          | Var1 (int) |———>| Declare  |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Var2 (char) |———>| Variables|
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Var3 (float) |———>| of      |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Var4 (double) |——->| Different |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Ptr1 (int*) |———>| Declare  |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Ptr2 (char*) |———>| Pointers |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Ptr3 (float*) |———>| (match   |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Ptr4 (double*) |——->| data     |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Ptr1 = &Var1 |———>| Assign   |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Ptr2 = &Var2 |———>| address  |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Ptr3 = &Var3 |———>| of       |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Ptr4 = &Var4 |———>| variables|
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Print Var1, Ptr1 |———>| Print    |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Print Var2, Ptr2 |———>| values   |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Print Var3, Ptr3 |———>| and      |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Print Var4, Ptr4 |———>| addresses|
          +———+         +———+
                 |                 |
                 v                 v
          +———+
          | End     |
          +———+

Practical Related Questions

1. Write the output of the following program:

#include <stdio.h>
void main() {
    int i = 5;
    int *p = &i;
    int **q = &p;
    int ***r = &q;
    printf(“%d %d %dn”, *p, **q, ***r); 
}
Answer:
5 5 5
2. Write the value of d in the following component of the program: 
int a=20;
int*b=
&a;
int*c=
b;
int d=*b+*c;

Answer:

value of d = 40

Conclusion

We successfully solved practical no.24 of Programming in C 312303 manual in which we learned about Writing C Program to print addresses and values of variables using Pointer

Leave a Reply

Your email address will not be published. Required fields are marked *