Pointer Operations and Arrays Manipulation in C Programming

basic operations on pointers and pointers n.w
1 / 9
Embed
Share

Learn about basic operations, pointer expressions, pointer variables, and allowed pointer operations in C programming. Understand how pointers can be used in various scenarios, such as assignments, comparisons, arithmetic operations, and shorthand operators, with examples provided. Master the manipulation of arrays using pointers for efficient memory management in C.

  • C Programming
  • Pointer Operations
  • Arrays
  • Pointers
  • Memory Management

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. BASIC OPERATIONS ON Pointers AND POINTERS TO ARRAYS 3/20/2025 CSE 1001 Department of CSE 1

  2. Pointers- recap int int Quantity int int * * p p; ; p p = = &Quantity &Quantity; ; //assigns address of variable Quantity to pointer p recap Quantity; ; //defines variable Quantity of type int //defines p as a pointer to int Now Now Quantity Quantity = = 50 *p *p = = 50 50; ; //assigns 50 to Quantity 50; ;//assigns 50 to Quantity 3/20/2025 CSE 1001 Department of CSE 2

  3. Pointer expressions Pointers can be used in most valid C expressions. However, some special rules apply. You may need to surround some parts of a pointer expression with parentheses in order to ensure that the outcome is what you desire. As with any variable, a pointer may be used on the right side of an assignment operator to assign its value to another pointer. 3/20/2025 CSE 1001 Department of CSE 3

  4. Pointer Expressions - Example Eg: int a=10, b=20,c,d=10; int *p1 = &a, *p2 = &b; Expression a b c c= *p1**p2; OR *p1 * *p2 OR (*p1) * (*p2) c= c + *p1; 10 20 200 10 20 210 c=5 * - *p2 / *p1; OR ( 5 * (- (*p2)))/(*p1) //space between / and * is required 10 20 -10 *p2 =*p2 +10; 10 30 3/20/2025 CSE 1001 Department of CSE 4

  5. Operations on Pointer Variables Assignment the value of one pointer variable can be assigned to another pointer variable of the same type Relational operations - two pointer variables of the same type can be compared for equality, and so on Some limited arithmetic operations integer values can be added to and subtracted from a pointer variable value of one pointer variable can be subtracted from another pointer variable Shorthand Increment and Decrement Operators 3/20/2025 CSE 1001 Department of CSE 5

  6. Allowed Pointer Operations - Example Assume an integer occupies 4 bytes int a = 10, b = 20, *p1, *p2, *p3, *p4; p1 = &a; //assume address of a = 2004 p2 = &b; //assume address of b = 1008 Pointer Operations Example expression Result Addition of integers from pointers p3 = p1 + 2 value of p3 = 2004 + 4*2 = 2012 Subtraction of integers from pointers p4 = p2 2 value of p4 = 1008-4*2 = 1000 Subtraction of one pointer from another c = p3 p1 Value of c = 2012 2004= 2 Pointer Increment p1++ Value of p1 = 2008 Pointer Decrement --p1 Value of p1 = 2004 3/20/2025 CSE 1001 Department of CSE 6

  7. Allowed Pointer Operations - Example if (p1<p2) printf( p1 points to lower memory than p2 ); if (p1==p2) printf( p1 and p2 points to same location ); if (p1!=p2) printf( p1 and p2 NOT pointing to same location ); 3/20/2025 CSE 1001 Department of CSE 7

  8. Invalid Operations: Pointers are not used in division and multiplication. p1/p2; p1*p2; p1/3; are not allowed. Two pointers can not be added. p1 + p2 is illegal. 3/20/2025 CSE 1001 Department of CSE 8

  9. Program to exchange two values #include< #include<stdio.h int int main() main() { { stdio.h> > int int x, y, t, *a, *b; x, y, t, *a, *b; a=&x; b=&y; a=&x; b=&y; printf printf("Enter the values of a and b: ("Enter the values of a and b: \ \n"); scanf scanf("%d %d", a, b); // equivalent to ("%d %d", a, b); // equivalent to scanf t=*a; t=*a; *a=*b; *a=*b; *b=t; *b=t; n"); scanf( %d %d , &x, &y); ( %d %d , &x, &y); Enter the values of a and b: Enter the values of a and b: 10 5 10 5 x= 5 x= 5 y = 10 y = 10 printf printf("x = %d ("x = %d \ \n", x); printf printf("y = %d", y); ("y = %d", y); n", x); return 0; return 0; } } 3/20/2025 CSE 1001 Department of CSE 9

More Related Content