0% found this document useful (0 votes)
4 views

Pointers and Pointer Operators in C (1)

Uploaded by

25f1001525
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Pointers and Pointer Operators in C (1)

Uploaded by

25f1001525
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Pointers and Pointer

Operators in C
by Prakhar mishra
Increment Operators: Incrementing Pointer
Values
Pre-increment (++p) Post-increment (p++)

Increments the pointer value before using it. Increments the pointer value after using it.

int *p = &x; // p points to x int *p = &x; // p points to x


++p; // p now points to the next integer after p++; // p now points to the next integer after
x x
Decrement Operators: Decrementing Pointer
Values
Pre-decrement (--p) Post-decrement (p--)

Decrements the pointer value before using it. Decrements the pointer value after using it.

int *p = &x; // p points to x int *p = &x; // p points to x


--p; // p now points to the integer before x p--; // p now points to the integer before x
Addition Operator: Adding an Integer to a
Pointer
Adding an integer to a pointer

Moves the pointer forward in memory by the specified number of elements.

int *p = &x; // p points to x


p += 2; // p now points to the integer 2 elements after x
Subtraction Operator: Subtracting an Integer
from a Pointer
Subtracting an integer from a pointer

Moves the pointer backward in memory by the specified number of elements.

int *p = &x; // p points to x


p -= 2; // p now points to the integer 2 elements before x
Comparison of Same Type of Pointers:
Comparing Pointer Values
Relational Operators (==, !=, <, >, <=, >=)

Compare the addresses stored in pointers of the same type.

int *p1 = &x; // p1 points to x


int *p2 = &y; // p2 points to y
if (p1 == p2) { ... } // Check if p1 and p2 point to the same location
Assignment of Pointers of the Same Type:
Assigning Pointer Values
Assigning pointer values

Copies the address stored in one pointer to another pointer of the same type.

int *p1 = &x; // p1 points to x


int *p2 = p1; // p2 now also points to x
Code Examples and Memory Representations
Example Code Memory Representation

Before:
x: 10 at address 0x1000
int x = 10; y: 20 at address 0x1004
z: 30 at address 0x1008
int y = 20;
int z = 30; After:
int *p = &x; // p points to x (address of x) x: 10 at address 0x1000
y: 20 at address 0x1004
int *q = &y; // q points to y (address of y) z: 30 at address 0x1008
printf("Address of x: %p\n", &x); p points to 0x1008
printf("Address of y: %p\n", &y); q points to 0x1008
printf("Address of z: %p\n", &z);
printf("Value pointed to by p: %d\n", *p);
p++; // p now points to the next integer after x
printf("Value pointed to by p: %d\n", *p);
p += 2; // p now points to the integer 2 elements after x
printf("Value pointed to by p: %d\n", *p);
if (p == q) {
printf("p and q point to the same location\n");
}
q = p; // q now also points to the same location as p
printf("Value pointed to by q: %d\n", *q);
Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy