0% found this document useful (0 votes)
15 views23 pages

Unit5 Pointers

Uploaded by

0gouthamtg
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)
15 views23 pages

Unit5 Pointers

Uploaded by

0gouthamtg
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/ 23

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.

March 29, 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.

March 29, 2024 CSE, BMSCE 2


Declaring Pointer variable
DataType *Pointer_var_name;

March 29, 2024 CSE, BMSCE 3


Declaring Pointer variable
DataType *Pointer_var_name;

Example:
int *ptr;
int Var;

Var=10;
ptr=&Var;

March 29, 2024 CSE, BMSCE 4


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

a=38;
p=&a;

March 29, 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);

March 29, 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

March 29, 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);
}

March 29, 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

March 29, 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 ;

March 29, 2024 CSE, BMSCE 10


#include <stdio.h>
int main()
{

int *p;
char *cp;
float *fp;
printf(" size of char =%d\n",sizeof(char));
printf(" size of float =%d\n",sizeof(float));
printf(" size of double =%d\n",sizeof(double));
printf(" size of integer pointer p =%d\n",sizeof(p));
printf(" size of character pointer p =%d\n",sizeof(cp));
printf(" size of floating pointer p =%d\n",sizeof(fp));
return 0;
}
March 29, 2024 CSE, BMSCE 11
Write a c program to test whether a number is
positive, negative ,or equal to zero

March 29, 2024 CSE, BMSCE 12


#include <stdio.h>
int main()
{
int i=10;
int *ptr;
ptr=&i;
printf(" VALUE OF i =%d\n",i);
printf("VALUE OF ptr=%d\n",ptr);
printf("VALUE OF &i= %d\n",&i);
printf("VALUE OF *ptr = %d\n",*ptr);
printf("VALUE of *(&i)= %d\n",*(&i));
printf("VALUE OF &ptr =%d\n",&ptr);
return 0;
}

March 29, 2024 CSE, BMSCE 13


 Like other variables, pointer variables can also be used in expressions.
 For example, if ptr1 and ptr2 are pointers, then the following statements
are valid.
 int num1=2 ,num2=3, sum=0, mul=0, div=1;
 int *ptr1,*ptr2;
 ptr1 = &num1;
 ptr2 = &num2;
 Sum = *ptr1 + *ptr2;
 Mul = sum * *ptr1;
 *ptr2 +=1;
 In C, programmers may add or subtract integers from pointers. We can
also subtract one pointer from the other.
 We can also use shorthand operators as we use with other variables.

March 29, 2024 CSE, BMSCE 14


 C also allows to compare pointers by using relational
operators in the expressions.
 For example,
 p1 > p2
 p1 ==p2
 p1! = p2 are all valid in C.

March 29, 2024 CSE, BMSCE 15


 When using pointers, unary (++0 increment and unary (--)
decrement operators have higher
 precedence than *(dereference operator). Therefore the
expression,
 *ptr++
 Is equivalent to *(ptr++) as ++ has higher precedence than
*. Thus the expression will first increase the
 value by 1, (pointing to a next memory location).Thus
doesn’t perform the intended task.
 The correct way of writing is:
 (*ptr)++
March 29, 2024 CSE, BMSCE 16
Lets now summarize the rules of pointers operation:

1. A pointer variable can be assigned the address of another variable (of the same
datatype).
2. A pointer variable can be assigned the value of another variable (of same
datatype).
3. A pointer variable can be initialized with the null value.
4. Prefix or postfix increment or decrement operators can be applied on a pointer
variable.
5. An integer value can be added or subtracted from a pointer variable.
6. A pointer variable can be compared with another pointer variable of the same
type using relational operators.
7. A pointer variable cannot be multiplied by a constant.
8. A pointer variable cannot be added to another pointer variable.

March 29, 2024 CSE, BMSCE 17


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;
}

March 29, 2024 CSE, BMSCE 18


Arithmetic Operations on Pointer variables

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

p=&u;
q=&v;

March 29, 2024 CSE, BMSCE 19


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);


}

March 29, 2024 CSE, BMSCE 20


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
}

March 29, 2024 CSE, BMSCE 21


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);
}

March 29, 2024 CSE, BMSCE 22


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
}

March 29, 2024 CSE, BMSCE 23

You might also like