Pointer Assignment 1 — Basic Concept & Dereferencing
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program: Demonstrate the Basic Concept of Pointers — Declaring and Dereferencing a Pointer Variable

📑 On this page:
  • What a Pointer Actually Is
  • The Two Key Operators: & and *
  • Complete C Program
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions
📚 In this program, you will learn:
  • How to declare an ordinary int variable and a pointer to it
  • How to store a variable's address using the & (address-of) operator
  • How to read a pointed-to value using the * (dereference) operator
  • How writing through a dereferenced pointer modifies the original variable
  • Why the pointer's own address is different from the address it holds

What a Pointer Actually Is

Every variable in a C program has two things: a value and an address. When you write int num = 42;, the compiler reserves some bytes in memory for num and, at runtime, stores 42 into those bytes. The bytes have a numeric location — the address — that the program can retrieve with the & operator.

A pointer is simply a variable whose value is an address. Declaring int *ptr; means: "ptr is a variable that will hold the address of an int." After ptr = #, the pointer holds the address of num. You can then use *ptr to talk to the memory at that address — either to read the current value or to overwrite it.

It's helpful to picture two boxes. One box holds 42 and is labelled num. A second, smaller box holds the address of the first box and is labelled ptr. The pointer points to the variable; it does not copy the value.

💡 In one line: A pointer stores an address. The & operator gets an address; the * operator follows an address to reach the value.

C program diagram showing a variable num and pointer ptr pointing to it, illustrating the basic concept of pointers

The Two Key Operators: & and *

Operator Name What it does Example
&Address-ofReturns the memory address of its operand.p = #
*DereferenceAccesses the value stored at the address held by the pointer.x = *p;
*p = valueWrite via pointerOverwrites the memory the pointer points to.*p = 99;
pPointer itselfThe address the pointer holds (not the pointed-to value).printf("%p", (void*)p);
&pAddress of pointerWhere the pointer variable itself lives in memory.printf("%p", (void*)&p);

Two traps to avoid from the start. First, p and *p are different things — p is an address, *p is a value. Second, the pointer's own address (&p) is unrelated to the address it stores. p lives somewhere in memory; the address in it belongs to a different object.

Complete C Program

Program: declare a pointer, store an address, then read and modify the value through it

#include <stdio.h>

int main(void) {
    /* 1. An ordinary integer variable */
    int num = 42;

    /* 2. A pointer to an int — currently uninitialized */
    int *ptr = NULL;

    /* 3. Store the address of num in the pointer */
    ptr = &num;

    /* 4. Print everything to make the concept visible */
    printf("Value of num           = %d\n", num);
    printf("Address of num (&num)   = %p\n", (void *)&num);
    printf("Value stored in ptr    = %p\n", (void *)ptr);
    printf("Address of ptr (&ptr)   = %p\n", (void *)&ptr);
    printf("Value pointed to (*ptr)= %d\n", *ptr);

    /* 5. Modify num indirectly through the pointer */
    *ptr = 99;

    printf("\nAfter *ptr = 99:\n");
    printf("Value of num           = %d\n", num);
    printf("Value pointed to (*ptr)= %d\n", *ptr);

    /* 6. Modify num directly and observe *ptr follows */
    num = 7;

    printf("\nAfter num = 7:\n");
    printf("Value of num           = %d\n", num);
    printf("Value pointed to (*ptr)= %d\n", *ptr);

    return 0;
}
                                    

Program Output

Value of num           = 42
Address of num (&num)   = 0x7ffd5c1e4a3c
Value stored in ptr    = 0x7ffd5c1e4a3c
Address of ptr (&ptr)   = 0x7ffd5c1e4a30
Value pointed to (*ptr)= 42

After *ptr = 99:
Value of num           = 99
Value pointed to (*ptr)= 99

After num = 7:
Value of num           = 7
Value pointed to (*ptr)= 7
                                

The exact addresses will differ on your machine — stack addresses vary per run and per platform. What matters is the relationship: &num and ptr print the same address, because the pointer was initialized with the variable's address. Meanwhile &ptr is a different address entirely — that's where the pointer variable itself lives. After *ptr = 99, num shows 99: the write through the pointer reached the same memory the variable occupies. After num = 7, *ptr shows 7: the pointer still points to num, so it reflects any change.

Step-by-Step Explanation

Let's walk through the program line by line.

1. int num = 42;

The compiler reserves space for an int (usually 4 bytes) and stores 42 in it. The variable num now has a value (42) and an address (some location on the stack). Both exist simultaneously. You can read the value as num and the address as &num.

2. int *ptr = NULL;

This declares ptr as a pointer to int and initializes it to NULL. A pointer holds an address, so a fresh pointer with no target should not be left uninitialized — that would be a wild pointer, pointing to an arbitrary location. Setting it to NULL means "pointing to nothing," which is safe to test with if (ptr != NULL) before dereferencing.

3. ptr = &num;

The & operator asks the compiler "give me the address of num." That address is assigned to ptr. From this moment, ptr and &num hold the same numeric value. The pointer now points to num.

4. The five printf statements

Each line exposes a different piece of the picture. num prints the value; &num prints the address; ptr prints the address it holds — which is the same as &num. &ptr prints the address of the pointer variable itself, which is different. Finally *ptr dereferences the pointer — it goes to the address stored in ptr and reads whatever is there, which is 42. Format specifier %p is used for addresses; the cast to void * is needed because %p formally expects a pointer to void.

5. *ptr = 99;

On the left of =, *ptr means "the memory location that ptr points to." Assigning 99 writes to that location. Since ptr points to num, this writes 99 into num. The subsequent printf confirms both num and *ptr show 99. The variable was changed without ever writing num = 99 directly. This is the essence of indirection — you reached the object through a pointer rather than by name.

6. num = 7;

A direct assignment to num updates its value to 7. Because ptr still holds the address of num, dereferencing shows 7. The pointer's target hasn't moved — the value in that target changed. This illustrates that pointers do not copy the value they see; they reference the object.

🎯 Remember this: A pointer variable is itself an object with an address. When you write printf("%p", ptr), you print the address it holds. When you write printf("%p", &ptr), you print where the pointer lives. These are two different numbers — beginners often confuse them.

7. Why initialize the pointer to NULL?

If ptr were left uninitialized, it would contain whatever value happened to be at its stack slot — possibly a valid-looking address but pointing to random memory. Dereferencing it would be undefined behaviour and might crash the program. Setting ptr = NULL makes the "no target yet" state explicit and testable. It costs nothing and prevents a whole category of bugs. Some compilers with strict warnings will flag an uninitialized pointer; NULL silences that and documents intent.

8. Where do these addresses come from?

Both num and ptr are auto variables declared inside main(), so they live on the call stack. Their addresses are meaningful only for the duration of main(). This is why you should never return the address of a local variable from a function — by the time the caller receives it, the frame may already have been reused. For this program, the addresses live from the moment of declaration until main() returns, which is sufficient.

9. What about the format specifier?

Pointers are printed with %p. Strictly, %p expects a void * argument, so passing an int * triggers a warning under -Wall. Casting with (void *) is the standard way to satisfy that. You could use %lx with a cast to unsigned long, but %p is idiomatic and portable, and it prints in a format most systems accept.

10. What's next?

Once you can freely declare, initialize, and dereference a pointer to a scalar, the natural next step is passing pointers to functions so a callee can modify the caller's variable. That's exactly the next assignment in this series — swapping two numbers using pointers.

Frequently Asked Questions

1. What is the difference between ptr and *ptr?

ptr is the pointer itself — its value is an address. *ptr dereferences it: it accesses whatever is stored at that address. If ptr = &num and num = 42, then ptr is the address of num and *ptr is 42. Changing *ptr writes through the pointer to num; changing ptr only makes it point somewhere else.

2. Why do I need & when calling scanf?

scanf("%d", &x) needs to modify x from inside the function. C is pass-by-value, so to let scanf change a variable in the caller, you pass its address. Inside scanf, the argument is a pointer to the caller's x, and it writes through that pointer. This is the same principle this program demonstrates — pointer writes change the caller's object. For strings (already arrays/pointers) you don't need &.

3. Is a pointer type just a number?

At the machine level, yes — a pointer is typically an integer-sized memory address. But the pointer's type matters at compile time. int *, char *, and double * all store addresses, but the compiler uses the type to know how many bytes to read when you dereference and by how much to advance when you do pointer arithmetic. Dereferencing a char * reads one byte; a double * reads eight on most systems.

4. What happens if I forget to initialize the pointer?

It becomes a wild pointer — its value is whatever happened to be in that stack slot. Dereferencing it may read garbage, may crash, or worse may silently corrupt memory. Always initialize pointers. If you don't know the target yet, set them to NULL and check for NULL before dereferencing.

5. Can the pointer point to a different variable later?

Absolutely — that's normal. If you later write int other = 100; ptr = &other;, the pointer now holds other's address and *ptr equals 100. The pointer itself doesn't remember num. It just stores an address and follows whatever it currently points at. Reassignment is fine as long as the new target lives as long as the pointer does.

6. What does (void *) do in the printf?

It casts the pointer to void *, which is what %p formally expects. Without the cast, some compilers warn about a type mismatch. The cast doesn't change the address value — it only reassures the compiler about the type. void * is the generic pointer type that any object pointer can convert to.

💡 Tip: Once this program runs correctly, try a variation: declare two ints, two int pointers, and swap the pointers (not the values). Then print both variables. You'll see the pointer swap doesn't affect the variables — because you swapped which addresses the pointers hold, not the values at those addresses. That puzzle is exactly why the next assignment — swapping values via pointers — is worth studying carefully.

📖 Related Tutorials

  • What Are Pointers In C?
  • Declaration Of A Pointer
  • The Initialization Of A Pointer
  • The & Address-Of Operator In C
  • Accessing Pointers — Indirectly and Directly
  • 2. Swap Two Numbers Using Pointers
  • The Null Pointer
  • The Wild Pointer

Previous Topic: -->> Pointers Assignments Index   ||   Next Topic: -->> 2. Swap Two Numbers Using Pointers


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.