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

PSP Unit 5 Pointers

Pointers are variables that store the address of other variables. A pointer variable contains the address of the variable it points to. Pointer arithmetic and dereferencing allow manipulating data using pointers. Arithmetic operations can be performed on pointer variables similar to regular variables.

Uploaded by

sonikasharma.cse
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)
29 views

PSP Unit 5 Pointers

Pointers are variables that store the address of other variables. A pointer variable contains the address of the variable it points to. Pointer arithmetic and dereferencing allow manipulating data using pointers. Arithmetic operations can be performed on pointer variables similar to regular variables.

Uploaded by

sonikasharma.cse
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/ 17

Pointers

 A pointer is a variable which stores the address


of another variable.
 A pointer is a derive data type in ‘C’.
 Pointers can be used to access and manipulate
data stored in memory.

May 20, 2024 CSE, BMSCE 1


Advantages of Pointers
 Pointers are more efficient in handling arrays and data
tables.
 Pointers can be used to return multiple values from a
function.
 Pointers allow ‘C’ to support dynamic memory
management.
 Pointers provide an efficient tool for manipulating
dynamic data structures such as stack, queue etc.

May 20, 2024 CSE, BMSCE 2


Declaring Pointer variable
DataType *Pointer_var_name;

May 20, 2024 CSE, BMSCE 3


Declaring Pointer variable
DataType *Pointer_var_name;

Example:
int *ptr;
int Var;

Var=10;
ptr=&Var;

May 20, 2024 CSE, BMSCE 4


Example Program
#include <stdio.h>
main() {
int a, *p;

a=38;
p=&a;

May 20, 2024 CSE, BMSCE 5


Example Program
#include <stdio.h>
main() {
int a, *p;

a=38;
p=&a;

printf("&a=%x a=%d\n", &a,a);


printf("p=%x *p=%d\n",p,*p);

May 20, 2024 CSE, BMSCE 6


Example Program
#include <stdio.h>
main() {
int a, *p;

a=38;
p=&a;

printf("&a=%x a=%d\n", &a,a);


printf("p=%x *p=%d\n",p,*p);

Output:
}
&a=fb6f9c a=38
p=fb6f9c *p=38

May 20, 2024 CSE, BMSCE 7


Question
Find the output of following program
#include <stdio.h>
main() {
int a, *p;
float b, *q;

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

b=47.5;
q=&b;
printf("&b=%x b=%f\n",&b,b);
printf("q=%x *q=%f\n",q,*q);
}

May 20, 2024 CSE, BMSCE 8


Question
Find the output of following program
#include <stdio.h>
main() {
int a, *p;
float b, *q;

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

b=47.5;
q=&b; Output:
printf("&b=%x b=%f\n",&b,b); &a=a0e98050 a=38
printf("q=%x *q=%f\n",q,*q); p=a0e98050 *p=38
} &b=a0e98054 b=47.50
q=a0e98054 *q=47.50

May 20, 2024 CSE, BMSCE 9


Pointer Expressions and Pointer Arithmetic

Write a program to find area and printf ( "Enter radius of a circle " ) ;
perimeter of a circle using pointers scanf ( "%d", &radius ) ;

#include<stdio.h> areaperi ( radius, &area, &perimeter ) ;


void areaperi ( int r, float *a, float *p )
{ printf ( "Area = %f", area ) ;
*a = 3.14 * r * r ; printf ( "Perimeter = %f", perimeter ) ;
*p = 2 * 3.14 * r ; }
}
Output
void main( )
{
int radius ;
float area, perimeter ;

May 20, 2024 CSE, BMSCE 10


Write a c program to test whether a number is
positive, negative ,or equal to zero

May 20, 2024 CSE, BMSCE 11


Program to add two numbers using pointers

#include <stdio.h>

int main()
{
int num1, num2, sum;
int *ptr1, *ptr2;

ptr1 = &num1; // ptr1 stores the address of


num1
ptr2 = &num2; // ptr2 stores the address of
num2

printf("Enter any two numbers: ");


scanf("%d%d", ptr1, ptr2);

sum = *ptr1 + *ptr2;

printf("Sum = %d", sum);

return 0;
}

May 20, 2024 CSE, BMSCE 12


Arithmetic Operations on Pointer variables

Example Program
#include <stdio.h>
main() {
int u=20, v=5;
int *p, *q;

p=&u;
q=&v;

May 20, 2024 CSE, BMSCE 13


Arithmetic Operations on Pointer variables

Example Program
#include <stdio.h>
main() {
int u=20, v=5;
int *p, *q;

p=&u;
q=&v;

printf("%d %d\n", *p+*q, u+v);


}

May 20, 2024 CSE, BMSCE 14


Arithmetic Operations on Pointer variables

Example Program
#include <stdio.h>
main() {
int u=20, v=5;
int *p, *q;

p=&u;
q=&v;

Output:
printf("%d %d\n", *p+*q, u+v);
25 25
}

May 20, 2024 CSE, BMSCE 15


Arithmetic Operations on Pointer variables

Example Program
#include <stdio.h>
main() {
int u=20, v=5;
int *p, *q;

p=&u;
q=&v;

printf("%d %d\n", *p+*q, u+v);


printf(“%d\n”,*p*u);
}

May 20, 2024 CSE, BMSCE 16


Arithmetic Operations on Pointer variables

Example Program
#include <stdio.h>
main() {
int u=20, v=5;
int *p, *q;

p=&u;
q=&v;

Output:
printf("%d %d\n", *p+*q, u+v);
25 25
printf(“%d\n”,*p*u);
400
}

May 20, 2024 CSE, BMSCE 17

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