C Programming • Pointers & File handling
C Programming / Pointers: Idea of pointers

Pointers: Idea of pointers

Notes 5 Pointers & File handling

A pointer is a variable that stores the address of another variable. Since every variable is stored at some memory location, a pointer can be used to keep the address of that location and access the value stored there.

Notes

A pointer is a variable that stores the address of another variable. Since every variable is stored at some memory location, a pointer can be used to keep the address of that location and access the value stored there.

💡 Example
int age = 20; int *ptr; ptr = &age;

Here, age stores the value 20, while ptr stores the address of age.

Variable and Memory Address

Whenever a variable is declared, the computer allocates a memory location for it. The variable has a value, and that memory location has an address. The actual address may be different each time the program runs, so the important thing is the relationship between the variable and its address.

💡 Example
int marks = 75;

The value 75 is stored somewhere in memory and that memory location has an address.

Address of a Variable

The address of a variable is the memory location where its value is stored. In C, the address of a variable can be obtained using the address-of operator (&).

💡 Example
int marks = 75; printf("%p", (void *)&marks);

The expression &marks gives the address of the variable marks.

Address-of Operator (&)

The & operator is called the address-of operator. It returns the memory address of the variable placed after it. This address can then be stored in a pointer variable of the appropriate type.

💡 Example
int number = 10; &number

&number represents the address of number, not the value 10.

Pointer Variable

A pointer variable is a variable specifically designed to store a memory address. The type of the pointer tells the compiler what kind of value is expected at the address stored in that pointer.

💡 Example
int *ptr;

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

Value-at-Address Operator (*)

The * operator can be used to obtain the value stored at the address contained in a pointer. It is called the value-at-address or indirection operator.

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

*ptr gives the value stored at the address held by ptr, which is 10.

Relationship Between Variable, Address and Pointer

A normal variable stores a value, while a pointer stores the address of that variable. Using the pointer with the * operator allows the program to reach the value stored at that address.

Variable ↓ Stores a value &Variable ↓ Gives its address Pointer ↓ Stores that address *Pointer ↓ Gives the value at that address
📌 Example
int i = 3; int *j; j = &i; i → value stored = 3 &i → address of i j → stores address of i *j → value stored at that address = 3

Why Do We Need Pointers?

Pointers allow a program to work directly with memory addresses. They are useful when a program needs to access or modify data indirectly, pass addresses to functions, work with arrays and strings, or manage memory dynamically.

💡 Example

Instead of passing the value of a variable to a function, a program can pass its address so that the function can work with the original variable.

Important Difference Between & and *

The & operator is used to obtain an address, whereas the * operator is used to access the value stored at an address held by a pointer. Understanding this difference is the foundation of pointer programming.

Expression Meaning
&i Address of variable i.
p Address stored in pointer p.
*p Value stored at the address held by p.
*( &i ) Value stored at the address of i.

Practical Example

Problem Statement

Write a C program to demonstrate the relationship between a variable, its address, a pointer, and the value stored at the pointer's address.

Learning Outcomes

  • Understand the meaning of a memory address.
  • Use the address-of operator &.
  • Declare and initialize a pointer.
  • Use the indirection operator * to access a value.

Hint

Declare an integer variable, store its address in an integer pointer, and display the address and value through both the variable and the pointer.

Theory

A pointer stores the address of another variable. The address-of operator gives the address of a variable, while the indirection operator accesses the value stored at that address.

Program

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

Expected Output

Value of number = 25

Address of number = 0x7ffe12345678

Value stored in ptr = 0x7ffe12345678

Value at address stored in ptr = 25

Note

The address shown in the output is only an example. The actual address will normally be different each time the program runs. What remains important is that ptr contains the same address as &number, and *ptr gives the value stored there.

Pointer Declaration Examples

The pointer type should correspond to the type of data whose address it is intended to store. This tells the compiler how the pointed-to data should be interpreted.

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

p points to an integer, ch points to a character, and f points to a floating-point value.

Common Mistake

A pointer stores an address, not the ordinary data value itself. For example, assigning number directly to a pointer is not the correct way to make the pointer point to number. Its address should be assigned instead.

⚠️ Example

Correct: ptr = &number;

Incorrect: ptr = number;

Quick Revision

Concept Remember
Pointer A variable that stores an address.
& Address-of operator.
* Value-at-address / indirection operator.
int *p Pointer to an integer.
p = &i Stores the address of i in p.
*p Accesses the value at the address stored in p.

Important Exam Questions

Short Answer Questions

  1. What is a pointer in C?
  2. What is the address-of operator?
  3. What is the value-at-address operator?
  4. What does int *p mean?
  5. How can the address of a variable be obtained?
  6. What does *p represent?

Long Answer Questions

  1. Explain the concept of pointers in C with a suitable example.
  2. Explain the relationship between a variable, its address and a pointer.
  3. Explain the address-of and indirection operators with examples.
  4. Write a C program to demonstrate the use of a pointer variable.
🎥 Recommended Learning

Watch a beginner-friendly explanation of pointers in C.

▶ Watch: Pointers in C — Hindi

📝 Handwritten Notes

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

🧠 Mind Map

Use the mind map for variable, address, pointer, & and * relationships.