C Programming • Pointers & File handling
C Programming / Defining pointers

Defining pointers

Notes 5 Pointers & File handling

A pointer variable must be declared before it is used. The declaration tells the compiler that the variable will store the address of a particular type of data. The * symbol in a pointer declaration indicates that the variable is a pointer.

Notes

A pointer variable must be declared before it is used. The declaration tells the compiler that the variable will store the address of a particular type of data. The * symbol in a pointer declaration indicates that the variable is a pointer.

💡 Example
int *ptr;

Here, ptr is declared as a pointer to an integer. It is meant to store the address of an int variable.

Basic Syntax

The general form of a pointer declaration is:

data_type *pointer_name;
💡 Example
int *p; char *ch; float *f;

Each declaration creates a pointer for a different data type.

Integer Pointer

An integer pointer is used to store the address of an integer variable. Its declaration uses the int data type followed by * and the pointer name.

💡 Example
int number = 25; int *ptr; ptr = &number;

ptr stores the address of number.

Character Pointer

A character pointer stores the address of a character variable. The pointer is declared using the char type.

💡 Example
char grade = 'A'; char *ptr; ptr = &grade;

Here, ptr stores the address of the character variable grade.

Floating-Point Pointer

A floating-point pointer stores the address of a float variable. It is declared using the float type.

💡 Example
float percentage = 82.5; float *ptr; ptr = &percentage;

Here, ptr stores the address of percentage.

Pointer Initialization

Declaring a pointer only creates the pointer variable. Before using it to access a variable, it should be initialized with a valid address of a compatible variable.

💡 Example
int number = 10; int *ptr = &number;

The pointer ptr is initialized with the address of number.

Using the Same Pointer Type with the Same Data Type

A pointer should normally be used with the corresponding data type it is declared for. This allows the compiler to correctly interpret the value stored at the pointed-to address.

💡 Example
int number = 50; int *ptr = &number;

Here, an int * pointer is used with an int variable.

Multiple Pointer Declarations

More than one pointer can be declared in the same statement. Every variable that needs to be a pointer must have its own * symbol.

💡 Example
int *p, *q;

Both p and q are pointers to integers.

Pointer Declaration with a Normal Variable

A normal variable and a pointer variable can appear in the same declaration statement, but the * belongs only to the variable immediately associated with it.

⚠️ Example
int number, *ptr;

Here, number is an ordinary integer variable, whereas ptr is an integer pointer.

Meaning of the Asterisk in Pointer Declaration

In a declaration such as int *ptr, the asterisk indicates that ptr is a pointer to an integer. It does not mean that ptr will store an ordinary integer value.

💡 Example
int *ptr;

This means ptr is capable of storing the address of an integer value.

Pointer Declaration and Meaning

Declaration Meaning
int *p; p is a pointer to an integer.
char *p; p is a pointer to a character.
float *p; p is a pointer to a floating-point value.
double *p; p is a pointer to a double value.

Practical Example

Problem Statement

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

Learning Outcomes

  • Declare an integer pointer.
  • Initialize a pointer with the address of a variable.
  • Access the value through the pointer.

Hint

Declare an integer variable and an integer pointer. Assign &number to the pointer and use *ptr to access the stored value.

Theory

A pointer declaration specifies the type of value whose address the pointer is expected to store. After initialization, the pointer can be used with the indirection operator to access that value.

Program

#include <stdio.h> int main() { int number = 50; int *ptr; // store the address of number in the pointer ptr = &number; printf("Value of number = %d ", number); printf("Value through pointer = %d ", *ptr); return 0; }

Expected Output

Value of number = 50

Value through pointer = 50

Note

The pointer ptr does not contain the value 50 directly. It contains the address of number, and *ptr accesses the value stored at that address.

Common Mistakes

⚠️ Mistake 1

Declaring a pointer and using it before giving it a valid address can lead to undefined behavior.

⚠️ Mistake 2

Do not confuse int *ptr with *ptr. The first is a declaration, while the second is used to access the value at the stored address.

Quick Revision

Concept Remember
Pointer Declaration data_type *pointer_name;
Integer Pointer int *p;
Character Pointer char *p;
Initialization Store a compatible variable's address in the pointer.
Address Use the & operator.
Value Use the * operator with the pointer.

Important Exam Questions

Short Answer Questions

  1. What is a pointer declaration?
  2. What does int *p mean?
  3. What is an integer pointer?
  4. How is a pointer initialized?
  5. What is the meaning of char *p?
  6. Why must a pointer be initialized before it is used?

Long Answer Questions

  1. Explain pointer declaration and initialization with suitable examples.
  2. Explain different types of pointer declarations in C.
  3. Write a C program to declare, initialize and use an integer pointer.
🎥 Recommended Learning

Watch a beginner-friendly explanation of pointer declaration and initialization in C.

▶ Watch: Defining Pointers in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for pointer declarations will be provided here.

🧠 Mind Map

Use the mind map for pointer declaration, initialization, address and indirection.