Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

23/05/2023

Introduction
• A pointer is a variable that contains the
memory location of another variable.

C Pointers • A pointer can be pointed to any variable


type.
• The address operator (&) gives the
''address of a variable''.
• The indirection or dereference operator
(*) gives the ''contents of an object
pointed to by a pointer''.
© G.Y.Namani C Pointers Slide 1 © G.Y.Namani C Pointers Slide 2

Pointer declaration Pointer declaration cont…


• You start by specifying the type of • If you declare more than one pointer
data stored in the location identified by on the same line, you must precede
the pointer.
• The asterisk tells the compiler that you each of them with an asterisk.
are creating a pointer variable. • Syntax for declaring a point to a
• Finally you give the name of the variable:
pointer variable.
• data_type *variable_name;

© G.Y.Namani C Pointers Slide 3 © G.Y.Namani C Pointers Slide 4

Address operator examples


• Once we declare a pointer variable we /*The use of pointers in a C
must point it to something, we can do program*/
this by assigning to the pointer the #include<stdio.h>
address of the variable you want to
point. int main(){ float
• E.g., ptr=&number; number1=4,number2=19, sum;
• This places the address where number /*declaration of pointer
is into the variable ptr. If number is variables*/
stored in memory 21260 address then float *ptr_a;
the variable ptr has the value 21260. float *ptr_b;
© G.Y.Namani C Pointers Slide 5 © G.Y.Namani C Pointers Slide 6

1
23/05/2023

Examples cont… Examples cont…


/*assigning the address of the /*output the sum with two
variable to a pointer*/ decimal places*/
ptr_a=&number1; printf("The sum of two
ptr_b=&number2; numbers is %.2f\n",sum);
/*using pointers in return 0;
expression*/
}
sum=*ptr_a+*ptr_b;

© G.Y.Namani C Pointers Slide 7 © G.Y.Namani C Pointers Slide 8

Examples cont… Examples cont…


*declaring pointers, using them /*declaration of pointer
in calculations and memory variables*/
locations*/
int *m,*n;
#include<stdio.h>
int main() {
/*pointing a pointer to
address of a variable*/
/*variable declaration and
initialization*/ m=&x;
int area, x=12, y=5; n=&y;
© G.Y.Namani C Pointers Slide 9 © G.Y.Namani C Pointers Slide 10

Examples cont… Pointer expressions


/*using pointer variable in • Like other variables pointer variables
expression*/ can be used in expressions.
area=*m**n; printf("The area of • For example, if p1 and p2 are properly
rectangle is %d\n",area); declared and initialized pointers, then
printf("\nThe address of x=%u and the following statements are valid:
y=%u\n",m,n); /*%u is format y=*p1**p2;
specifier for unsigned decimal sum=sum+*p1;
integer*/ z= 5 - *p2/*p1;
return 0; *p2= *p2 + 10;
}
© G.Y.Namani C Pointers Slide 11 © G.Y.Namani C Pointers Slide 12

2
23/05/2023

Pointer expressions cont…


• C allows us to add integers to or Thank you for listening!
subtract integers from pointers as well
as to subtract one pointer from the
other.
• We can also use short hand operators
with the pointers *p1+=sum;
sum+=*p2; etc.,

© G.Y.Namani C Pointers Slide 13 © G.Y.Namani C Pointers Slide 14

You might also like