C Programming • Pointers & File handling
C Programming / Defining Pointers — Exercises

Defining Pointers — Exercises

Practical 5 Pointers & File handling

Write a C program to declare an integer pointer, store the address of an integer variable in it and display the value using the pointer.

Practical / Solution

Defining Pointers — Exercise 1

Problem

Write a C program to declare an integer pointer, store the address of an integer variable in it and display the value using the pointer.

Program

#include <stdio.h> int main() { int number = 25; int *ptr; ptr = &number; printf("Value = %d\n", *ptr); return 0; }

Explanation

The declaration int *ptr defines a pointer that can store the address of an integer variable. The address of number is assigned using the address-of operator &.

Expected Output

Value = 25

Defining Pointers — Exercise 2

Problem

Write a C program to define a pointer to a floating-point variable and display its value.

Program

#include <stdio.h> int main() { float price = 125.50; float *ptr; ptr = &price; printf("Price = %.2f\n", *ptr); return 0; }

Explanation

A pointer must be declared with a compatible data type. Here, float *ptr stores the address of the floating-point variable price.

Expected Output

Price = 125.50

Defining Pointers — Exercise 3

Problem

Write a C program to define a character pointer and display the character stored in a variable.

Program

#include <stdio.h> int main() { char grade = 'A'; char *ptr; ptr = &grade; printf("Grade = %c\n", *ptr); return 0; }

Explanation

The declaration char *ptr creates a pointer suitable for storing the address of a character variable.

Expected Output

Grade = A

Defining Pointers — Exercise 4

Problem

Write a C program to initialize a pointer at the time of declaration and display the value of the variable.

Program

#include <stdio.h> int main() { int number = 100; int *ptr = &number; printf("Number = %d\n", *ptr); return 0; }

Explanation

A pointer can be initialized when it is declared. In this example, ptr immediately stores the address of number.

Expected Output

Number = 100

Defining Pointers — Exercise 5

Problem

Write a C program to define two integer pointers and display the values of two variables through them.

Program

#include <stdio.h> int main() { int a = 10; int b = 20; int *p1 = &a; int *p2 = &b; printf("First value = %d\n", *p1); printf("Second value = %d\n", *p2); return 0; }

Explanation

Multiple pointers can be declared independently. Each pointer stores the address of its corresponding variable.

Expected Output

First value = 10

Second value = 20

Defining Pointers — Exercise 6

Problem

Write a C program to define a pointer and assign it the address of different variables one after another.

Program

#include <stdio.h> int main() { int a = 15; int b = 30; int *ptr; ptr = &a; printf("First value = %d\n", *ptr); ptr = &b; printf("Second value = %d\n", *ptr); return 0; }

Explanation

A pointer variable can be reassigned to another variable of the same compatible type. After reassignment, dereferencing the pointer accesses the new variable.

Expected Output

First value = 15

Second value = 30

Defining Pointers — Exercise 7

Problem

Write a C program to define a pointer and initialize it with NULL. Check whether the pointer is NULL before using it.

Program

#include <stdio.h> int main() { int *ptr = NULL; if (ptr == NULL) { printf("Pointer is NULL.\n"); } else { printf("Pointer contains an address.\n"); } return 0; }

Explanation

A NULL pointer does not point to a valid object for dereferencing. Checking a pointer before using it can help avoid invalid memory access.

Expected Output

Pointer is NULL.

Defining Pointers — Exercise 8

Problem

Write a C program to define an integer pointer and display both the address stored in the pointer and the value at that address.

Program

#include <stdio.h> int main() { int number = 75; int *ptr = &number; printf("Address stored in pointer = %p\n", (void *)ptr); printf("Value at address = %d\n", *ptr); return 0; }

Explanation

The pointer stores the memory address of number. The dereference operator * accesses the value stored at that address.

Expected Output

Address stored in pointer = 0x7ff...

Value at address = 75

The exact memory address depends on the system.

Defining Pointers — Exercise 9

Problem

Write a C program to define pointers for integer, float and character variables and display their values.

Program

#include <stdio.h> int main() { int number = 50; float price = 75.25; char grade = 'B'; int *iptr = &number; float *fptr = &price; char *cptr = &grade; printf("Integer = %d\n", *iptr); printf("Float = %.2f\n", *fptr); printf("Character = %c\n", *cptr); return 0; }

Explanation

Pointer declarations use a data type corresponding to the type of object whose address they store. This example demonstrates int *, float * and char *.

Expected Output

Integer = 50

Float = 75.25

Character = B

Defining Pointers — Exercise 10

Problem

Write a C program to define an integer pointer and use it to update a variable after initialization.

Program

#include <stdio.h> int main() { int marks = 70; int *ptr = &marks; printf("Original marks = %d\n", marks); *ptr = 85; printf("Updated marks = %d\n", marks); return 0; }

Explanation

Once the pointer has been initialized with the variable's address, assigning a value to *ptr changes the original variable. This demonstrates the practical purpose of defining and initializing a pointer.

Expected Output

Original marks = 70

Updated marks = 85