Pointer Basics,Constant Pointers & Pointer to Constant.

MeghajKumarMallick 112 views 19 slides Apr 07, 2020
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

This is an PPT is an PPt of C++ Programming Language. This includes topics such as " Pointer Basics,Constant Pointers & Pointer to Constant"


Slide Content

BIRLA INSTITUTE OF TECHNOLOGY TOPIC :- POINTERS BASICS, CONSTANT POINTERS AND POINTERS TO CONSTANT DIVYANSH PARMARTHI MCA/25016/18 SHRADDHA MALVIYA MCA/25018/18

POINTERS A pointer is a variable that points a address of a variable. A pointer is declared using the * operator before an identifier.

Declaration of Pointer variables : type *pointer_name; where type is the type of data pointed to For example :- int *numberPointer ;

ADDRESS (&) OPERATOR Pointer address operator is denoted by ‘&’ symbol The & is a unary operator that returns the memory address of its operand . Example : & n - It gives an address on variable n

DEREFERENCING OPERATOR ( *) We can access to the value stored in the variable pointed to by using the dereferencing operator ( * ) Syntax: * PointerVariable Example: int V = 101; int *P = &V;

POINTERS TO POINTERS A pointer to a pointer is a chain of pointers.  In pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value. Pointer Pointer Variable Address Address Value

Declaration of pointer to a pointer : int ** ptr ; For example : // local declaration int a; int *p; int **q; // Statment a=58; p=&a; q=&p;

cout << a << “ “ << endl ; cout << *p << “ “ << endl ; cout << **q << “ “ << endl ; OUTPUT 58 58 58

POINTERS AS PARAMETERS To make changes to a variable that exist after a function ends, we pass the address of the variable to the function For example : #include< iostream.h > void Indirectswap (char *ptr1, char *ptr2) { char c = *ptr1; *ptr1 = *ptr2; *ptr2 = c; }

int main() { char a = ‘y’; char b = ‘n’; Indirectswap (& a,&b ); cout << a << “and”<< b << endl ; return 0; } OUTPUT n and y

CONSTANT POINTERS AND POINTERS TO CONSTANT BY :- SHRADDHA MALVIYA

Constant pointer Constant pointer cannot be modified. Modification in integer to which it points to it is allowed. Syntax: int n1=20; int *const ptr=&n1; //Declare constant

Example of constant pointer

Output

Pointer to constant Pointer to constant object is called as pointer to constant object. Syntax: int n=30; const int *ptr=&n; “const int” means integer constant. We cannot change value of integer. We can change address of such pointer so that it will point to new memory allocation

Example of pointer to constant

Output

Difference Constant pointer Pointer to constant Pointer can be incremented. Pointer is not incremented. Constant pointer is pointing to data object. Pointer is pointing to constant data object.t Declaration :int * const ptr; Declaration: const int *ptr;

THANK YOU
Tags