DAY12 Understanding C Pointers: Concepts, Operations, and Examples

Understanding C Pointers: Concepts, Operations, and Examples

1. What Is a Pointer and Why Is It Important?

Key Advantages of Using Pointers

  1. Efficient data access (indirect access)
  2. An alternative way to access variables (via memory address)
  3. Ability to manipulate low-level hardware (registers, memory-mapped I/O)
  4. Essential for building data structures (linked list, tree, graph)
  5. Efficient function parameter passing (pass by address)

2. Memory Address and Pointer Basics

Memory Address

Every byte in memory has an address, for example:

0x0000000012345678

This is simply an integer.

Pointer Variable Definition

A pointer is a variable used to store a memory address.

Example:

int a = 100;
printf("%p\n", &a);  // Print the memory address of a

On a 64-bit System:

All pointer types have the same size:

8 bytes (64 bits)

Regardless of whether they point to int, char, or double.


3. Pointer Operators: & (address) and * (dereference)

✔️ The & Operator — Get Address

int a = 100;
int* p = &a;

Notes:

❌ You cannot take the address of an expression
❌ You cannot take the address of a constant

&(a + 1)   // invalid
&10         // invalid

✔️ The * Operator — Dereference

Dereferencing means:

  1. Get the address stored in the pointer
  2. Interpret the memory using the pointer’s base type
  3. Access (read or write) the data
int a = 100;
int* p = &a;

printf("%d\n", *p);  // prints 100

*p = 200;            // writes 200 into memory of a

✔️ & and * Are Opposites

*p == a
&(*p) == p
*(&a) == a

4. Declaring and Initializing Pointers

Basic Syntax

base_type *pointer_name;

Examples:

int* p;
char* cp;
double* dp;

❗ Uninitialized Pointer = Wild Pointer (Dangerous)

int *p;
printf("%d\n", *p);  // undefined behavior

✔ Correct Initialization

int *p = NULL;

int a = 10;
p = &a;   // safe to dereference now

5. Basic Example: Pointer Operations

int a = 100;
printf("&a = %p\n", &a);
printf("sizeof(&a) = %lu\n", sizeof(&a));  // pointer size = 8 bytes

int* p = &a;
printf("*p = %d\n", *p);

*p = 200; // changes the value of a

6. Pointers and Arrays

The array name acts like the address of the first element:

int b[5] = {1, 2, 3, 4, 5};
int* p = b;     // same as p = &b[0]

Accessing elements using pointers:

*(p+0) == b[0]
*(p+1) == b[1]
*(p+2) == b[2]

Because pointer arithmetic is based on element size:

p + 1 = p + sizeof(int)

7. Pointer Arithmetic

p + 1 does NOT mean “add 1”

It means:

Move the pointer forward by one element.

For int (4 bytes):

p + 1 = p + 4

Example:

int a = 100;
int *p = &a;

printf("%p\n", p);
printf("%p\n", p + 1);

8. Using Pointers to Traverse an Array

Method 1: Do not change the pointer

for(int i=0; i<5; i++)
{
    printf("%d\n", *(p+i));
}

Method 2: Use p++ (changes pointer)

for(int i=0; i<5; i++)
{
    printf("%d\n", *(p++));
}

After the loop, p points to:

b[5] — out-of-bounds

9. Pointer Subtraction

Pointer subtraction gives the number of elements between them.

Example:

int b[5] = {1,2,3,4,5};
int* p = b;
int* q = &b[2];

printf("q - p = %ld\n", q - p);  // 2

10. Summary of Today’s Code

✔ Print address and dereference

int a = 100;
int* p = &a;
printf("%p %d\n", p, *p);

✔ Wild pointer example (incorrect)

int *p;
printf("%d\n", *p); // undefined behavior

✔ Pointer access to array

int b[5] = {1,2,3,4,5};
int *p = b;

for(int i=0;i<5;i++)
    printf("%d %d\n", i, *(p+i));

✔ Pointer increment

for(int i=0;i<5;i++)
    printf("%d %d\n", i, *(p++));
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值