Upload
gourav-kottawar
View
439
Download
3
Embed Size (px)
Citation preview
Contents6.1 Introduction 6.2 Memory Organization 6.3 Basics of Pointer 6.4 Application of Pointer 6.5 Pointer Expressions Declaration of Pointer, Initializing Pointer, De-referencing Pointer Void Pointer Pointer Arithmetic 6.6 Precedence of &, * operators 6.7 Pointer to Pointer 6.8 Constant Pointe
2
6.9 Dynamic Memory Allocation 6.10 sizeof(), malloc(), calloc(), realloc(), free() 6.11 Pointers and Arrays 6.12 Pointers and character string 6.13 Array of pointers
3
Pre-requisite Basics of the C programming
language Data type Variable Array Function call Standard Input/Output
e.g. printf(), scanf()
Pointers - Introduction Pointer is a derived type. Pointers contain memory address
as their values. Pointers can be used to access and
manipulate data stored in memory.
4
5
Computer Memory Revisited
Computers store data in memory slots
Each slot has an unique address Variables store their values like
this:Addr
Content
Addr
Content Addr
Content
Addr
Content
1000 i: 37 1001 j: 46 1002 k: 58 1003 m: 741004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘c’ 1007 a[3]: ‘\
0’1008 ptr:
10011009 … 1010 1011
6
Computer Memory Revisited
Altering the value of a variable is indeed changing the content of the memory e.g. i = 40; a[2] = ‘z’;
Addr
Content
Addr
Content Addr
Content
Addr
Content
1000 i: 40 1001 j: 46 1002 k: 58 1003 m: 741004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘z’ 1007 a[3]: ‘\
0’1008 ptr:
10011009 … 1010 1011
7
Addressing Concept Pointer stores the address of
another entity It refers to a memory locationint i = 5;
int *ptr; /* declare a pointer variable */
ptr = &i; /* store address-of i to ptr */
printf(“*ptr = %d\n”, *ptr); /* refer to referee of ptr */
8
Why do we need Pointer? Simply because it’s there! It is used in some circumstances in
CRemember this?
scanf(“%d”, &i);
Benefits of pointers More efficient in handling arrays and data
tables. Pointers can be used to return multiple
values from a function via function arguments.
Pointers permit references to functions and there by facilitating passing of functions as arguments to other functions.
The use of pointer arrays to character strings result in saving of data storage space in memory. 9
Benefits of Pointer Pointers allow C to support DMA. Pointers provides an efficient tool for
manipulating dynamic data structures such as structures, linked list, trees, etc.
Pointers reduce length and complexity program.
They increase execution speed and thus reduce the program execution time.
10
11
What actually ptr is? ptr is a variable storing an
address ptr is NOT storing the actual value
of iint i = 5;
int *ptr;
ptr = &i;
printf(“i = %d\n”, i);
printf(“*ptr = %d\n”, *ptr);
printf(“ptr = %p\n”, ptr);
5i
address of iptr
Output:i = 5
*ptr = 5
ptr = effff5e0
value of ptr = address of iin memory
12
Twin Operators
&: Address-of operator Get the address of an entity
e.g. ptr = &j;
Addr
Content
Addr
Content Addr
Content
Addr
Content
1000 i: 40 1001 j: 33 1002 k: 58 1003 m: 741004 ptr:
10011005 1006 1007
13
Twin Operators
*: De-reference operator Refer to the content of the referee
e.g. *ptr = 99;
Addr
Content
Addr
Content Addr
Content
Addr
Content
1000 i: 40 1001 j: 99 1002 k: 58 1003 m: 741004 ptr:
10011005 1006 1007
Initialization of pointer variables
ptr= &a;
16
So, Pointer is a variable just like other
variables of c but only difference is unlike the other variable it stores the memory address of any other variables of c. This variable may be type of int, char, array, structure, function or any other pointers.
Examples17
18
(1)Pointer p which is storing memory address of a int type variable:int i=50;
int *p=&i; (2)Pointer p which is storing memory address of an array:
int arr[20];int (*p)[20]=&arr;
(3)Pointer p which is storing memory address of a function:
char display(void);char(*p)(void)=&display;
19
(4)Pointer p which is storing memory address of struct type variable:
struct abc{int a;float b;}var;struct abc *p=&var;
20
Example: Pass by Reference
Modify behaviour in argument passing
void f(int j){ j = 5;}void g(){ int i = 3; f(i);}
void f(int *ptr){ *ptr = 5;}void g(){ int i = 3; f(&i);} i = ?i = ?i = 3 i = 5
21
An Illustrationint i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data TableName Type Description Value
i int integer variable 5j int integer variable 10
22
An Illustrationint i = 5, j = 10;
int *ptr; /* declare a pointer-to-integer variable */
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data TableName Type Description Value
i int integer variable 5j int integer variable 10
ptr int * integer pointer variable
23
An Illustrationint i = 5, j = 10;
int *ptr;
int **pptr; /* declare a pointer-to-pointer-to-integer variable */
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data TableName Type Description Value
i int integer variable 5j int integer variable 10
ptr int * integer pointer variablepptr int
**integer pointer pointer variable
Double Indirection
24
An Illustrationint i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i; /* store address-of i to ptr */
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data TableName Type Description Value
i int integer variable 5j int integer variable 10
ptr int * integer pointer variable address of ipptr int
**integer pointer pointer variable
*ptr int de-reference of ptr 5
25
An Illustrationint i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr; /* store address-of ptr to pptr */
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data TableName Type Description Value
i int integer variable 5j int integer variable 10
ptr int * integer pointer variable address of ipptr int
**integer pointer pointer variable
address of ptr
*pptr int * de-reference of pptr value of ptr(address of
i)
26
An Illustrationint i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data TableName Type Description Value
i int integer variable 3j int integer variable 10
ptr int * integer pointer variable address of ipptr int
**integer pointer pointer variable
address of ptr
*ptr int de-reference of ptr 3
27
An Illustrationint i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data TableName Type Description Value
i int integer variable 7j int integer variable 10
ptr int * integer pointer variable address of ipptr int
**integer pointer pointer variable
address of ptr
**pptr int de-reference of de-reference of pptr
7
28
An Illustrationint i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data TableName Type Description Value
i int integer variable 7j int integer variable 10
ptr int * integer pointer variable address of jpptr int
**integer pointer pointer variable
address of ptr
*ptr int de-reference of ptr 10
29
An Illustrationint i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data TableName Type Description Value
i int integer variable 7j int integer variable 9
ptr int * integer pointer variable address of jpptr int
**integer pointer pointer variable
address of ptr
**pptr int de-reference of de-reference of pptr
9
30
An Illustrationint i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data TableName Type Description Value
i int integer variable 7j int integer variable 9
ptr int * integer pointer variable address of ipptr int
**integer pointer pointer variable
address of ptr
*pptr int * de-reference of pptr value of ptr(address of
i)
31
An Illustrationint i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data TableName Type Description Value
i int integer variable -2j int integer variable 9
ptr int * integer pointer variable address of ipptr int
**integer pointer pointer variable
address of ptr
*ptr int de-reference of ptr -2
Chain of pointers
32
Address 2
Address 1 value
P2 p1 variable
The pointer variable p2 contains the address of the pointer variable p1 which points to the location that contains the desired value. This is known as multiple indirection.
•Example
examplemain(){ int a,b,*p1,*p2,x,y,z;
a=12;b=4; p1=&a;p2=&b;x=*p1 * *p2 – 6;y=4* - *p2 / * p1+10;printf(“Address of a = %u”,p1);printf(“\n Address of b = %u”,p2);printf(“a= %d, b= %d”.a,b); printf(“x= %d , y= %d”,x,y);*p2 = *p2 +3;*p1= *p2 – 5;z= *p1 * *p2 – 6;printf(“\n a = %d , b = %d”, a,b);printf(“\n z= %d”,z);
}
34
Pointer Increment and scale factor
characters 1 byte Integers 2 bytes Floats 4 bytes Long integers 4 bytes Doubles 8 bytes
35
Rules of pointer variable A pointer variable can be assigned the address of another
varaible. A pointer variable can be assigned the values of another
pointer variable. A pointer variable can be initialized with NULL or zero
value. A pointer variable can be pre-fixed or post-fixed with
increment or decrement operators. An integer value may be added or subtracted from a
pointer variable. When two pointers point to the same array, one pointer
variable can be subtracted from other. When two pointers point to the same objects of the same
data types, they can compared using relational operators. Two pointer variable can not be added A value can not be assigned to an arbitrary address. i.e
x= &10;
36
37
Pointer Arithmetic What’s ptr + 1? The next memory location! What’s ptr - 1? The previous memory location! What’s ptr * 2 and ptr / 2? Invalid operations!!!
38
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data TableName Type Description Valuea[0] float float array element
(variable)?
a[1] float float array element (variable)
?
a[2] float float array element (variable)
?
a[3] float float array element (variable)
?
ptr float *
float pointer variable
*ptr float de-reference of float pointer variable
?
39
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data TableName Type Description Valuea[0] float float array element
(variable)?
a[1] float float array element (variable)
?
a[2] float float array element (variable)
?
a[3] float float array element (variable)
?
ptr float *
float pointer variable address of a[2]
*ptr float de-reference of float pointer variable
?
40
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data TableName Type Description Valuea[0] float float array element
(variable)?
a[1] float float array element (variable)
?
a[2] float float array element (variable)
3.14
a[3] float float array element (variable)
?
ptr float *
float pointer variable address of a[2]
*ptr float de-reference of float pointer variable
3.14
41
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data TableName Type Description Valuea[0] float float array element
(variable)?
a[1] float float array element (variable)
?
a[2] float float array element (variable)
3.14
a[3] float float array element (variable)
?
ptr float *
float pointer variable address of a[3]
*ptr float de-reference of float pointer variable
?
42
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data TableName Type Description Valuea[0] float float array element
(variable)?
a[1] float float array element (variable)
?
a[2] float float array element (variable)
3.14
a[3] float float array element (variable)
9.0
ptr float *
float pointer variable address of a[3]
*ptr float de-reference of float pointer variable
9.0
43
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data TableName Type Description Valuea[0] float float array element
(variable)?
a[1] float float array element (variable)
?
a[2] float float array element (variable)
3.14
a[3] float float array element (variable)
9.0
ptr float *
float pointer variable address of a[0]
*ptr float de-reference of float pointer variable
?
44
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data TableName Type Description Valuea[0] float float array element
(variable)6.0
a[1] float float array element (variable)
?
a[2] float float array element (variable)
3.14
a[3] float float array element (variable)
9.0
ptr float *
float pointer variable address of a[0]
*ptr float de-reference of float pointer variable
6.0
45
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data TableName Type Description Valuea[0] float float array element
(variable)6.0
a[1] float float array element (variable)
?
a[2] float float array element (variable)
3.14
a[3] float float array element (variable)
9.0
ptr float *
float pointer variable address of a[2]
*ptr float de-reference of float pointer variable
3.14
46
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data TableName Type Description Valuea[0] float float array element
(variable)6.0
a[1] float float array element (variable)
?
a[2] float float array element (variable)
7.0
a[3] float float array element (variable)
9.0
ptr float *
float pointer variable address of a[2]
*ptr float de-reference of float pointer variable
7.0
47
Pointer Arithmetic and Array
Type of a is float * a[2] *(a + 2)
ptr = &(a[2]) ptr = &(*(a + 2)) ptr = a + 2
a is a memory address constant
ptr is a pointer variable
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
49
More Pointer Arithmetic What if a is a double array? A double may occupy more memory
slots! Given double *ptr = a; What’s ptr + 1 then?
Addr
Content Addr
Content Addr
Content
Addr
Content
1000 a[0]: 37.9
1001 … 1002 … 1003 …
1004 a[1]: 1.23
1005 … 1006 … 1007 …
1008 a[2]: 3.14
1009 … 1010 … 1011 …
50
More Pointer Arithmetic Arithmetic operators + and – auto-
adjust the address offset According to the type of the pointer:
1000 + sizeof(double) = 1000 + 4 = 1004
Addr
Content Addr
Content Addr
Content
Addr
Content
1000 a[0]: 37.9
1001 … 1002 … 1003 …
1004 a[1]: 1.23
1005 … 1006 … 1007 …
1008 a[2]: 3.14
1009 … 1010 … 1011 …
Array of string Pointer to array of string: A pointer which pointing to
an array which content is string, is known as pointer to array of strings.#include<stdio.h>int main(){char *array[4]={"c","c++","java","sql"};char *(*ptr)[4]=&array;printf("%s ",++(*ptr)[2]);return 0;}
51
Array of string Pointer to array of string: A pointer which pointing to
an array which content is string, is known as pointer to array of strings.#include<stdio.h>int main(){\char *array[4]={"c","c++","java","sql"};char *(*ptr)[4]=&array;printf("%s ",++(*ptr)[2]);return 0;}
52
Output: ava
example#include<stdio.h>int main(){
static char *s[3]={"math","phy","che"};typedef char *( *ppp)[3];static ppp p1=&s,p2=&s,p3=&s;char * (*(*array[3]))[3]={&p1,&p2,&p3};char * (*(*(*ptr)[3]))[3]=&array;p2+=1;p3+=2;printf("%s",(***ptr[0])[2]);
return 0;}
54
example#include<stdio.h>int main(){
static char *s[3]={"math","phy","che"};typedef char *( *ppp)[3];static ppp p1=&s,p2=&s,p3=&s;char * (*(*array[3]))[3]={&p1,&p2,&p3};char * (*(*(*ptr)[3]))[3]=&array;p2+=1;p3+=2;printf("%s",(***ptr[0])[2]);
return 0;}
55
Output: che
As we know p[i]=*(p+i)(***ptr[0])[2]=(*(***ptr+0))[2]=(***ptr)[2]
=(***(&array))[2] //ptr=&array=(**array)[2] //From rule *&p=p=(**(&p1))[2] //array=&p1=(*p1)[2]=(*&s)[2] //p1=&s
=s[2]=”che”
56
Exampleint main(){ int i,j,temp1,temp2; int arr[8]={5,3,0,2,12,1,33,2}; int *ptr; for(i=0;i<7;i++){ for(j=0;j<7-i;j++){if(*(arr+j)>*(arr+j+1)){ ptr=arr+j; temp1=*ptr++; temp2=*ptr; *ptr--=temp1; *ptr=temp2;} } }
57
exampleint main(){static float farray[]
[3]={0.0f,1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f};float (*array[3])[3]={&farray[0],&farray[1],&farray[2]};float (*(*ptr)[])[3]=&array;printf("%f ",2[(*(**ptr+1))]);return 0;}
58
exampleint main(){static float farray[]
[3]={0.0f,1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f};float (*array[3])[3]={&farray[0],&farray[1],&farray[2]};float (*(*ptr)[])[3]=&array;printf("%f ",2[(*(**ptr+1))]);return 0;}
59
Output: 5.000000
Dynamic memory Allocation
Malloc –Use - The C library function void *malloc(size_t
size) allocates the requested memory and returns a pointer to it
Syntax - void *malloc(size_t size)Where size -- This is the size of the memory
block, in bytes.Return Value - This function returns a pointer to
the allocated memory, or NULL if the request fails.
60
exampleint main() { char *str; /* Initial memory allocation */
str = (char *) malloc(15); strcpy(str, "tutorialspoint"); printf("String = %s, Address = %u\n", str, str); return(0); }
61
exampleint main() { char *str; /* Initial memory allocation */
str = (char *) malloc(15); strcpy(str, "tutorialspoint"); printf("String = %s, Address = %u\n", str, str); return(0); }
62
Output - String = tutorialspoint, Address = 355090448 String = tutorialspoint.com, Address = 355090448
Dynamic Memory Allocation Calloc () –
The C library function void *calloc(size_t nitems, size_t size) allocates the requested memory and returns a pointer to it. The difference in malloc and calloc is that malloc does not set the memory to zero where as calloc sets allocated memory to zero.
Syntax - void *calloc(size_t nitems, size_t size) Parametersnitems -- This is the number of elements to be allocated.size -- This is the size of elements. Return ValueThis function returns a pointer to the allocated memory, or
NULL if the request fails.
63
exampleint main() { int i, n; int *a; printf("Number of elements to be entered:"); scanf("%d",&n); a = (int*)calloc(n, sizeof(int)); printf("Enter %d numbers:\
n",n); for( i=0 ; i < n ; i++ ) { scanf("%d",&a[i]); } printf("The numbers entered are: "); for( i=0 ; i < n ; i++ ) { printf("%d ",a[i]); } return(0); }
64
Free()The C library function void
free(void *ptr) deallocates the memory previously allocated by a call to calloc, malloc, or realloc.
67
DMA functions realloc()Use- can reduce or maximize previously
allocated memorySyntax – ptr = realloc(ptr,newsize);
68
Generic pointervoid pointer in c is known as generic pointer.
Literal meaning of generic pointer is a pointer which can point type of data.
Example:
void *ptr;Here ptr is generic pointer.
69
Generic pointerint main(){void *ptr;printf("%d",*ptr);return 0;}
71
Output: Compiler error
1. We cannot dereference generic pointer.
Generic pointer
int main(){void *ptr;printf("%d",sizeof(ptr));return 0;}
72
Output: 2Explanation: Size of any type of near pointer in c
is two byte.
We can find the size of generic pointer using sizeof operator.
Generic Pointerint main(){char c='A';int i=4;void *p;char *q=&c;int *r=&i;p=q;printf("%c",*(char *)p);p=r;printf("%d",*(int *)p);
return 0;}
73
Output: A4
Generic pointer can hold any type of pointers like char
pointer, struct pointer, array of pointer etc
without any typecasting.
Generic Pointer
4. Any type of pointer can hold generic pointer without any typecasting.
5. Generic pointers are used when we want to return such pointer which is applicable to all types of pointers. For example return type of malloc function is generic pointer because it can dynamically allocate the memory space to stores integer, float, structure etc. hence we type cast its return type to appropriate pointer type
74
Null pointerLiteral meaning of NULL pointer is a pointer which is
pointing to nothing. NULL pointer points the base address of segment.
Examples of NULL pointer:
1. int *ptr=(char *)0;2. float *ptr=(float *)0;3. char *ptr=(char *)0;4. double *ptr=(double *)0;5. char *ptr=’\0’;6. int *ptr=NULL;
75
Null PointerWhat is meaning of NULL?
NULL is macro constant which has been defined in the heard file stdio.h, alloc.h, mem.h, stddef.h and stdlib.h as
76
int main(){if(!NULL)printf("I know preprocessor");elseprintf("I don't know preprocessor");return 0;
}Output: I know preprocessor
Explanation:!NULL = !0 = 1
77
int main(){int i;
static int count;for(i=NULL;i<=5;){
count++;i+=2;}
printf("%d",count);return 0;
}
78
Output: 3
Example – Null pointerint main(){#ifndef NULL#define NULL 5#endif
printf("%d",NULL+sizeof(NULL));return 0;
}
79
Output: 2Explanation:
NULL+sizeof(NULL)=0+sizeoof(0)
=0+2 //size of int data type is two byte.
int main(){char *str=NULL;
strcpy(str,"c-pointer.blogspot.com");printf("%s",str);
return 0;}
80
Output: (null)
We cannot copy any thing
in the NULL pointer.
Wild pointerWild pointer:
A pointer in c which has not been initialized is known as wild pointer.
81
Output: Any addressGarbage value#include<stdio.h>
int main(){int *ptr;printf("%u\n",ptr);printf("%d",*ptr);return 0;}
Wild Vs Null
82
There is difference between the NULL pointer and wild pointer. Null pointer points the base address of segment while wild pointer doesn’t point any specific memory location.
Dangling pointer:
If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location whilepointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.
83
Near pointer:
The pointer which can points only 64KB data segment or segment number 8 is known as near pointer.
85
Near Pointer
86
•Near pointer cannot access beyond the data segment like graphics video memory, text video memory etc. •Size of near pointer is two byte. •With help keyword near, we can make any pointer as near pointer.
Near pointerint main(){int x=25;int near* ptr;ptr=&x;printf(“%d”,sizeof ptr)
;return 0;
}
87
Output: 2
Far pointer
The pointer which can point or access whole the residence memory of RAM i.e. which can access all 16 segments is known as far pointer
88
Far pointerint main(){
int x=10;int far *ptr;ptr=&x;printf("%d",sizeof ptr)
;return 0;
}
90
Output: 4
Limitation of far pointer:We cannot change or modify the segment address of given far address by applying any arithmetic operation on it. That is by using arithmetic operator we cannot jump from one segment to other segment. If you will increment the far address beyond the maximum value of its offset address instead of incrementing segment address it will repeat its offset address in cyclic order.
Huge Pointer The pointer which can point or
access whole the residence memory of RAM i.e. which can access all the 16 segments is known as huge pointer.
91