This is an PPT is an PPt of C++ Programming Language. This includes topics such as " Pointer Basics,Constant Pointers & Pointer to Constant"
Size: 488.35 KB
Language: en
Added: Apr 07, 2020
Slides: 19 pages
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;
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;