Apply for Zend Framework Certification Training

c




< Dynamic memory allocation Last >



Pointers in C – Detailed Notes with Examples
Pointers are one of the most powerful and important concepts in C. They allow direct access to memory and are widely used in arrays, functions, dynamic memory allocation, and data structures.
1) Understanding Memory Addresses
Every variable in C is stored in memory, and each memory location has a unique address.
Example:
#include <stdio.h>
int main() {
    int x = 10;
    printf("Value of x: %d\n", x);
    printf("Address of x: %p\n", &x);
    return 0;
}
Explanation:
x = 10 → value stored in memory.
&x → gives memory address of x.
%p → format specifier used to print addresses.
Memory looks like this (conceptually):
Address Value
1000 10
2) What is a Pointer?
A pointer is a variable that stores the address of another variable.
Syntax:
datatype *pointer_name;
Example:
int *ptr;
This means:
ptr can store the address of an integer variable.
3) Declaration and Initialization of Pointers
Step 1: Declare a pointer
int *ptr;
Step 2: Initialize the pointer
int x = 20;
int *ptr = &x;
 
Complete Example:
#include <stdio.h>
int main() {
    int x = 20;
    int *ptr = &x;
    printf("Value of x: %d\n", x);
    printf("Address of x: %p\n", &x);
    printf("Pointer ptr stores: %p\n", ptr);
    printf("Value at address stored in ptr: %d\n", *ptr);
    return 0;
}
Output (example):
Value of x: 20
Address of x: 0x7ffc1234
Pointer ptr stores: 0x7ffc1234
Value at address stored in ptr: 20
4) Pointer Operators
There are two important pointer operators:
 
Operator Name Purpose
& Address-of Operator Gets address of variable
* Dereference Operator Gets value at stored address
4.1  Address-of Operator (&)
Used to get the memory address of a variable.
Example:
int a = 5;
printf("%p", &a);
&a gives address of a.
 
4.2  Dereference Operator (*)
Used to access the value stored at the address held by a pointer.
Example:
int a = 5;
int *ptr = &a;
printf("%d", *ptr);
*ptr gives value stored at address → 5
5) Complete Working Example
#include <stdio.h>
int main() {
    int num = 50;
    int *p;
    p = &num;
    printf("num = %d\n", num);
    printf("Address of num = %p\n", &num);
    printf("p = %p\n", p);
    printf("Value using pointer = %d\n", *p);
    return 0;
}
6) Changing Value Using Pointer
Pointers can modify original variable values.
#include <stdio.h>
int main() {
    int x = 100;
    int *ptr = &x;
    *ptr = 200;   // changing value using pointer
    printf("Updated value of x: %d\n", x);
    return 0;
}
Output:
Updated value of x: 200
Because *ptr refers to original variable x.
7) Pointer and Memory Diagram
Suppose:
int x = 10;
int *p = &x;
Conceptual Memory Representation:
Variable Address Value
x 1000 10
p 2000 1000
p stores address of x
*p gives value at address 1000 → 10
 
8) Important Rules About Pointers
Pointer must be initialized before use.
Pointer type must match variable type.
int *p;      // correct for int
float *p;    // correct for float
Uninitialized pointer → garbage value (dangerous).
NULL pointer:
int *p = NULL;
9) Common Mistake Example
? Wrong:
int *p;
*p = 10;   // ERROR (p not initialized)
? Correct:
int x;
int *p = &x;
*p = 10;
10) Practice Example (With Output)
#include <stdio.h>
int main() {
    int a = 5, b = 10;
    int *p1 = &a;
    int *p2 = &b;
    printf("Before swap: a = %d, b = %d\n", a, b);
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;
    printf("After swap: a = %d, b = %d\n", a, b);
    return 0;
}
Output:
Before swap: a = 5, b = 10
After swap: a = 10, b = 5
 
Summary
Pointer = variable that stores memory address.
& → gives address.
* → gives value at address.
Pointers allow direct memory access.
Used in arrays, functions, dynamic memory, data structures.

< Dynamic memory allocation Last >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top