C Pointers

1. What is a Pointer?

  • A pointer is a variable that stores the memory address of another variable.

  • Instead of holding a value directly, it holds where the value is stored.

Syntax:



 

  • data_type → type of the variable the pointer points to

  • * → indicates the variable is a pointer


2. Declaring and Using Pointers


 

Output (example):

Value of num: 50
Address of num: 0x7ffee3b2c8ac
Value stored in ptr (address): 0x7ffee3b2c8ac
Value pointed by ptr: 50

*ptr is used to access the value stored at the address (dereferencing).


3. Changing Value Using Pointers


 

Output:

Value of num = 20

Using pointers, you can modify the variable directly via its address.


4. Pointer and Arrays

  • The name of an array is treated as a pointer to its first element.


 

Output:

Element 0 = 10
Element 1 = 20
Element 2 = 30
Element 3 = 40
Element 4 = 50

5. Pointer to Pointer

  • A pointer can store the address of another pointer.


 

Output:

Value of num = 100

6. Pointers and Functions (Call by Reference)


 

Output:

num = 15

Pointers allow functions to modify variables outside their scope.


7. Key Points About Pointers

  1. Declaration: data_type *ptr;

  2. Address-of operator &: stores the memory address in pointer.

  3. Dereference *: access or modify the value at the pointer.

  4. Arrays and pointers are closely related.

  5. Pointers are essential for:

    • Dynamic memory allocation

    • Passing arguments by reference

    • Linked lists, trees, and other data structures

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *