Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tejalal Choudhary “C Programming from Scratch” Pointers

Similar presentations


Presentation on theme: "Tejalal Choudhary “C Programming from Scratch” Pointers"— Presentation transcript:

1 Tejalal Choudhary “C Programming from Scratch” Pointers
Department of Computer Science & Engg SDBCT, Indore

2 Contents What is pointer Printing values using pointer
Call by reference Pointer arithmetic array and pointer Structure and pointer

3 Int a=10; Int b=4; char ch=‘A’ 1 2 3 4 5 6 7 12 13 18 24 19 a … …
65529 65524 65535 65530 b ch

4 Recalling variable What does above declaration mean? int a=10;
a is an integer variable Reserves 2 bytes in memory Associate the name a with memory location a will have some address

5 Address of Operator(&)
The & operator is used to access the address of a variable The %u is used to print the address with & int a=10; Following statement will print the address of variable a printf(“Address of a=%u”, &a);

6 Value at address operator(*)
Gives the value stored at a particular address Also known as indirection operator. int a=10; The following statements will print the value of a printf(“Value of a=%d”, a); //10 printf(“Value of a=%d”, *(&a)); //10

7 What is a pointer ? Declaration: int *p;
Pointers are variables that contain addresses Pointers would always contain whole numbers. A pointer when incremented always points to an immediately next location of its type. A pointer is a variable which holds/stores the address of another variable Declaration: int *p; p is a pointer variable which can store address of any integer variable Pointer variable can not store normal values p=3456; // not allowed P=&a; //allowed if a is an integer variable

8 int i = 3; int *j; int **k ; j=&i; K=&j;

9 } j = &i ; k = &j ; printf ( "\nAddress of i = %u", &i ) ;
int i = 3, *j, **k ; j = &i ; k = &j ; printf ( "\nAddress of i = %u", &i ) ; printf ( "\nAddress of i = %u", j ) ; printf ( "\nAddress of i = %u", *k ) ; printf ( "\nAddress of j = %u", &j ) ; printf ( "\nAddress of j = %u", k ) ; printf ( "\nAddress of k = %u", &k ) ; printf ( "\nValue of j = %u", j ) ; printf ( "\nValue of k = %u", k ) ; printf ( "\nValue of i = %d", i ) ; printf ( "\nValue of i = %d", * ( &i ) ) ; printf ( "\nValue of i = %d", *j ) ; printf ( "\nValue of i = %d", **k ) ; } 65524 65524 65524 65522 65522 65520 65524 65522 3 3 3 3

10 Call by reference s=sum(&a, &b);
Arguments can be passed to functions in one of the two ways: Sending the values of the arguments Sending the addresses of the arguments int a=10, b=20, s; s=sum(&a, &b); Function definition int sum(int *p, int *q) { int r=*p+*q; return r; } Passing the addresses of a & b We are passing addresses, we need pointer variable. p & q are pointer

11 Pointer arithmetic Addition of two pointers
Don’t attempt following operations on pointer Addition of two pointers Multiplication of a pointer with a constant Division of a pointer with a constant All of the above

12 What will be the output of the program?
void main(){  int i = 3, *x ; float j = 1.5, *y ; clrscr(); x = &i ; y = &j ; //suppose address of i=200 and address of j=400 x++ ; y++ ; printf( "\n %u ", x ) ; printf( " %u", y ) ;  getch();

13 What will be the output of the program?
#include<stdio.h> #include<conio.h> void main( ) { int i = 3, *j, **k ; clrscr(); j = &i ;//suppose address of i is 200 k = &j ;//suppose address of j is 400 printf ( "\n%u", j ) ; printf ( ",%u", k ) ; printf ( ",%d", * ( &i ) ) ; printf ( ",%d", **k ) ; getch(); }


Download ppt "Tejalal Choudhary “C Programming from Scratch” Pointers"

Similar presentations


Ads by Google