Date post: | 31-Oct-2014 |
Category: |
Engineering |
Upload: | rajat-bushehri |
View: | 101 times |
Download: | 5 times |
Saturday 8 April 2023
POINTERS
BY
GIRISH SHARMA RAJAT BUSHEHERI
{61264} {61279}
Saturday 8 April 2023
OVERVIEW
What pointers actually are…… How can we declare pointers…… Pointer to Pointer…… How many levels of pointers can you have? Pointers as function arguments…… Dynamic memory allocation using pointers……
Saturday 8 April 2023
What pointers actually are……
A pointer is a variable used to store the address of any memory cell, and can be used to access and manipulate data stored at the address to which it is pointing
Saturday 8 April 2023
Difference between pointer and variable:
◦Pointer holds the address
◦Variable holds the value.
Getting variable`s memory address …….
Different computers number this memory using different complex schemes.
We must happy ,as a programmer, We don’t need to know the particular address of any given variable because the compiler handles the details.
If we want this information , though, you can use the address-of operator(&),which returns the address of an object in memory.
Saturday 8 April 2023
More info …….
A pointer that is not initialized is called a wild pointer because you have no idea what it is pointing to.
A pointer whose value is zero is called a null pointer.
void pointer is a special type of pointer that can be pointed at objects of any data type! A void pointer is declared using data type void
Saturday 8 April 2023
How can we declare pointers :
Just like other variables ,pointers have to be declared before their usage.
Syntax :
data_type *pt;
ex : int *p;
Here p is a pointer which will store the address of the variable of type int .
Saturday 8 April 2023
int *p;
This tells the compiler three things about variable p :
The asterisk (*) tells that the variable p is a pointer variable.
p needs a memory location.
p points to a variable of type int.
Saturday 8 April 2023
How to initialize pointers :
The process of assigning address of a variable to a pointer variable is known as initialization
{ int a;
int *p; //declaration
p= &a; } //initialization
Saturday 8 April 2023
example :
int main()
{
int x= 10;
int *p ;
p= &x;
cout<<“value stored at x:”<<p;
}
Saturday 8 April 2023
Pointer to Pointer……
Saturday 8 April 2023
How many levels of pointers can you have?
int i = 0;
int *ip01 = & i; there is no limit , but just use that
int **ip02 = & ip01; that much levels which u can
int ***ip03 = & ip02; understand easily and programs
int ****ip04 = & ip03; programs does not become confusing.
int *****ip05 = & ip04;
int ******ip06 = & ip05;
int *******ip07 = & ip06;
int ********ip08 = & ip07;
int *********ip09 = & ip08;
Saturday 8 April 2023
Pointers as function arguments: #include <stdio.h>
swap (int *, int *);
main()
{
int a, b;
printf("\nEnter value of a & b: ");
scanf("%d %d", &a, &b);
printf("\nBefore Swapping:\n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(&a, &b);
printf("\nAfter Swapping:\n");
printf("\na = %d\n\nb = %d", a, b);getch();}swap (int *x, int *y){int temp;temp = *x;*x = *y;*y = temp;}
Saturday 8 April 2023
dynamic allocation of memory
Pointers need to be used for dynamic allocation of memory
Use the operator new to dynamically allocate space
Use the operator delete to later free this space
Saturday 8 April 2023
The new operator
If memory is available, the new operator allocates memory space for the requested object/array, and returns a pointer to (address of) the memory allocated.
If sufficient memory is not available, the new operator returns NULL.
The dynamically allocated object/array exists until the delete operator destroys it.
Saturday 8 April 2023
The delete operator
The delete operator deallocates the object or array currently pointed to by the pointer which was previously allocated at run-time by the new operator.the freed memory space is returned to Heapthe pointer is then considered unassigned
If the value of the pointer is NULL there is no effect.
Saturday 8 April 2023
Saturday 8 April 2023
Saturday 8 April 2023
Saturday 8 April 2023
Saturday 8 April 2023
Saturday 8 April 2023
Saturday 8 April 2023
Thanks ……
:)