Understanding Pointers in C
Understanding Pointers in C
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. For examples:
(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;
(4) Pointer p which is storing memory address of struct type variable:
struct abc{
int a;
float b;
}var;
struct abc *p=&var;
For pictorial explanation of pointer CLICK ME.
int a=5;
int * ptr;
ptr=&a;
Explanation:
About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 5
3. Address where it has stored in memory : 1025 (assume)
About variable ptr:
4. Name of variable : ptr
5. Value of variable which it keeps: 1025
6. Address where it has stored in memory : 5000 (assume)
Pictorial representation:
int a=50;
int *ptr1;
int **ptr2;
ptr1=&a;
ptr2=&pt1;
Explanation:
About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 50
3. Address where it has stored in memory : 5000 (assume)
About variable ptr1:
4. Name of variable : ptr1
5. Value of variable which it keeps: 5000
6. Address where it has stored in memory : 9000 (assume)
About variable ptr2:
7. Name of variable : ptr2
8. Value of variable which it keeps: 9000
9. Address where it has stored in memory : 9555 (assume)
Pictorial representation of above pointer declaration and
definition:
Note:
* is know as indirection operator which gives content of
any variable.
& is know as reference operator which gives address
where variable has stored in memory.
Cancellation rule of above two operators:
* and & operators always cancel to each other. i.e.
*&p=p
But it is not right to write:
&*p=p
Simple example:
int main(){
int x=25;
int *ptr=&x; //statement one
int **temp=&ptr; //statement two
printf(“%d %d %d”.x.*ptr,**temp);
return 0;
}
Output: 25 25 25
Explanation:
As we know value of variable x is 25.
*ptr= *(&x) //from statement one
=*&x
=x //using cancellation rule
=25
**temp= **(&ptr)=*(*&ptr)=*ptr=*(&x)=*&x=x=25
Where
(): This operator behaves as bracket operator or function operator.
[]: This operator behaves as array subscription operator.
*: This operator behaves as pointer operator not as multiplication operator.
Identifier: It is not an operator but it is name of pointer variable. You will
always find the first priority will be assigned to the name of pointer.
Data type: It is also not an operator. Data types also includes modifier (like
signed int, long double etc.)
Step 2: Inside the bracket * and ptr enjoy equal precedence. From rule of
associative (right to left) first priority goes to ptr and second priority goes to
*.
Step3: Assign third priority to [].
Step4: Since data type enjoys least priority so assign fourth priority to char.
Now read it following manner:
ptr is pointer to such one dimensional array of size three which
content char type data.
(2) How to read following pointer?
float (* ptr)(int)
Answer:
Assign the priority considering precedence and associative.
Address++ = Address
Address-- = Address
++Address = Address
--Address = Address
(1)What will be output of following c program?
#include<stdio.h>
int main(){
ptr=ptr+1;
printf(" %u",ptr);
return 0;
}
Output: 1002
(2)What will be output of following c program?
#include<stdio.h>
int main(){
p=p+3;
printf(" %u",p);
return 0;
}
Output: 1024
int main(){
float array[5]={1.1f,2.2f,3.3f};
float(*ptr)[5];
ptr=&array;
printf("%u",ptr);
ptr=ptr+1;
printf(" %u",ptr);
return 0;
}
Output: 1000 1020
int main(){
ptr=ptr+2;
printf(" %u",ptr);
return 0;
}
Output: 1026
ptr=ptr-4;
printf(" %u",ptr);
return 0;
}
Output: 960
float * display(int,int);
int max=5;
int main(){
float *(*ptr)(int,int);
ptr=display;
(*ptr)(2,2);
printf("%u",ptr);
ptr=ptr+1;
printf(" %u",ptr);
return 0;
}
float * display(int x,int y){
float f;
f=x+y+max;
return &f;
}
If you will subtract two pointers result will be a number but number will not
simple mathematical subtraction of two addresses but it follow following
rule:
If two pointers are of same type then:
int main(){
int *p=(int *)1000;
int *temp;
temp=p;
p=p+2;
printf("%u %u\n",temp,p);
printf("difference= %d",p-temp);
return 0;
}
Explanation:
Here two pointer p and temp are of same type and both are pointing
to int data type varaible.
p-temp = (1004-1000)/sizeof(int)
=4/2
=2
int main(){
printf("Difference= %d",q-p);
return 0;
}
Explanation:
q-p=(2000-100)/sizeof(float)
=1000/4
=250
struct abc{
signed char c;
short int i;
long double l;
};
int main(){
printf("Difference= %d",q-p);
return 0;
}
Output: Difference= 76
Explanation:
q-p=(2000-1000)/sizeof(struct abc)
=1000/(1+2+10)
=1000/13
=76
int main(){
XXX *p,*q;
p=(XXX *)1000;
q=(XXX *)2000;
printf("Difference= %d",q-p);
return 0;
}
Explanation:
q-p=(2000-100)/max(4,2,4)
=1000/4
=250
(4)What will be output of following c program?
#include<stdio.h>
int main(){
q++;
q++;
printf("%u %u\n",p,q);
printf("Difference= %d",q-p);
return 0;
}
Explanation:
q-p=(1016-1000)/sizeof(const volatile)
= 16/ (2*4)
=2
int main(){
int i=5;
int *p=&i;
int *q=(int *)2;
printf("%d",p+q);
return 0;
}
Rule 4: We can use relation operator and condition operator between two
pointers.
a. If two pointers are near pointer it will compare only its offset address.
int main(){
if(p==q)
printf("Equql");
else
printf("Not equal");
return 0;
}
Output: Equal
b. If two pointers are far pointer it will compare both offset and segment
address.
int main(){
if(p==q)
printf("Equql");
else
printf("Not equal");
return 0;
}
c. If two pointers are huge pointer it will first normalize into the 20 bit actual
physical address and compare to its physical address.
What will be output of following c program?
#include<stdio.h>
int main(){
if(p==q)
printf("Equql");
else
printf("Not equal");
return 0;
}
Output: Equal
int i=5,j=10;
int *p=&i;
int *q=&j;
printf("%d",p|q);
return 0;
}
int main(){
int near*far*huge* p;
printf("%d",sizeof(p));
printf(" %d",sizeof(*p));
printf(" %d",sizeof(**p));
return 0;
}
Output: 4 4 2
(1) What will be output if you will execute following code?
#include<stdio.h>
int * function();
int main(){
ptr=&function;
x=(*ptr)();
printf("%d",*x);
return 0;
}
int *function(){
static int a=10;
return &a;
}
Output: 10
Explanation: Here function is function whose parameter is void data type
and return type is pointer to int data type.
x=(*ptr)()
=> x=(*&functyion)() //ptr=&function
=> x=function() //From rule *&p=p
=> x=&a
So, *x = *&a = a =10
int find(char);
int(*function())(char);
int main(){
int x;
int(*ptr)(char);
ptr=function();
x=(*ptr)('A');
printf("%d",x);
return 0;
}
int(*function())(char){
return find;
}
Output: 65
Explanation: Here function whose name is function which passing void data
type and returning another function whose parameter is char data type and
return type is int data type.
x=(*ptr)(‘A’)
=> x= (*function ()) (‘A’) //ptr=function ()
//&find=function () i.e. return type of function ()
=> x= (* &find) (‘A’)
=> x= find (‘A’) //From rule*&p=p
=> x= 65
int main(){
char *string;
int a=2;
float b=2.0l;
char *(*ptr)(int*,float *);
ptr=&call;
string=(*ptr)(&a,&b);
printf("%s",string);
return 0;
}
char *call(int *i,float *j){
static char *str="c-pointer.blogspot.com";
str=str+*i+(int)(*j);
return str;
}
Output: inter.blogspot.com
Explanation: Here call is function whose return type is pointer to character
and one parameter is pointer to int data type and second parameter is
pointer to float data type and ptr is pointer to such function.
str= str+*i+ (int) (*j)
=”c-pointer.blogspot.com” + *&a+ (int) (*&b)
//i=&a, j=&b
=”c-pointer.blogspot.com” + a+ (int) (b)
=”c-pointer.blogspot.com” +2 + (int) (2.0)
=”c-pointer.blogspot.com” +4
=”inter.blogspot.com”
int main(){
ptr=&display;
string=(*ptr)(string);
printf("%s",string);
return 0;
}
temp=temp+13;
*temp='\0';
return str;
}
Output: cquestionbak
Explanation: Here display is function whose parameter is pointer to
character and return type is also pointer to character and ptr is its pointer.
temp is char pointer
temp=temp+13
temp=’\0’
Above two lines replaces first dot character by null character of string of
variable string i.e.
"cquestionbank\0blogspot.com"
As we know %s print the character of stream up to null character.
int display();
int(*array[3])();
int(*(*ptr)[3])();
int main(){
array[0]=display;
array[1]=getch;
ptr=&array;
printf("%d",(**ptr)());
(*(*ptr+1))();
return 0;
}
int display(){
int x=5;
return x++;
}
Output: 5
Explanation:
In this example:
array []: It is array of pointer to such function which parameter is void and
return type is int data type.
ptr: It is pointer to array which contents are pointer to such function which
parameter is void and return type is int type data.
(**ptr)() = (** (&array)) () //ptr=&array
= (*array) () // from rule *&p=p
=array [0] () //from rule *(p+i)=p[i]
=display () //array[0]=display
(*(*ptr+1))() =(*(*&array+1))() //ptr=&array
=*(array+1) () // from rule *&p=p
=array [1] () //from rule *(p+i)=p[i]
=getch () //array[1]=getch
char *array[4]={"c","c++","java","sql"};
char *(*ptr)[4]=&array;
printf("%s ",++(*ptr)[2]);
return 0;
}
Output: ava
Explanation:
In this example
Note: In the above figure upper part of box represent content and lower part
represent memory address. We have assumed arbitrary address.
++(*ptr)[2]
=++(*&array)[2] //ptr=&array
=++array[2]
=++”java”
=”ava” //Since ptr is character pointer so it
// will increment only one byte
struct address{
char *name;
char street[10];
int pin;
}cus={"A.Kumar","H-2",456003},*p=&cus;
int main(){
printf("%s %s",p->name,(*p).street);
return 0;
}
union address{
char *name;
char street[10];
int pin;
};
int main(){
emp.name="ja\0pan";
p=&emp;
printf("%s %s",p->name,(*p).name);
return 0;
}
Output: ja ja
Explanation:
p is pointer to union address.
-> and (*). Both are same thing. These operators are used to access data
member of union by using union’s pointer.
%s is used to print the string up to null character i.e. ‘\0’
int main(){
int s=2,*r=&s,**q=&r,***p=&q;
printf("%d",p[0][0][0]);
return 0;
}
Output: 2
Explanation:
int main(){
int *p,q;
p=(int *)5;
q=10;
printf("%d",q+p);
return 0;
}
Output: 25
Explanation: If you will see intermediate file you will find following code:
#include<stdio.h>
void main(){
int **p,q;
p=(int **)5;
q=10;
printf("%d",q+p);
return 0;
}
Explanations:
Here q pointer and p is a number.
In c
So,
New address = old address + number * Size of data type to which pointer is
pointing.
= 5 + 10 * sizeof (*int)
= 5+10*2 = 25.
Note. We are assuming default pointer is near. Actually it depends upon
memory model.
int main(){
p2+=1;
p3+=2;
printf("%s",(***ptr[0])[2]);
return 0;
}
Output: che
Explanation:
Here
ptr: is pointer to array of pointer to string.
P1, p2, p3: are pointers to array of string.
Pictorial representation:
Note: In the above figure upper part of box represent content and lower part
represent memory address. We have assumed arbitrary address.
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”
int main(){
const array[2][3][3]={0,1,2,3,4,5,6,7,8,9,10,11,12};
int const (*ptr)[2][3][3]=&array;
printf("%d ",*(*(*ptr)[1]+2));
return 0;
}
Output: 11
Explanation:
In this example:
array [2][3][3]:It is three dimensional array and its content are constant
integers.
ptr: It is pointer to such three dimensional array whose content are constant
integer.
Pictorial representation:
Note: In the above figure upper part of box represent content and lower part
represent memory address. We have assumed arbitrary address.
I.e. array element at the 1*(3*3) +0(3) + 2=11th position starting from zero
which is 11.
void main(){
long array[][3]={7l,14l,21l,28l,35l,42l};
long int (*ptr)[2][3]=&array;
printf("%li ",-0[1[0[ptr]]]);
return 0;
}
Output: -28
Explanation:
-0[1[0[ptr]]]
=-1[0[ptr]][0] //From rule array[i]=i[array]
=-0[ptr][1][0]
=-ptr [0] [1] [0]
=-*ptr [0] [1] //From rule array[i]=*(array+i)
=-*(&array) [0] [1]
=-(&array) [0] [1][0]
=-(*&array)[1][0] //From rule *&p=p
=-array[1][0]
array[1][0] means 1*(3)+ 0 = 3rd element of array starting from zero i.e. 28
{
if(*(arr+j)>*(arr+j+1))
{
ptr=arr+j;
temp1=*ptr++;
temp2=*ptr;
*ptr--=temp1;
*ptr=temp2;
}
}
}
clrscr();
for(i=0;i<8;i++)
printf(" %d",arr[i]);
getch();
}
Output: 0 1 2 2 3 5 12 33
Examples:
#include <stdio.h>
int main(){
if(!NULL)
printf("I know preprocessor");
else
printf("I don't know preprocessor");
return 0;
}
#include <stdio.h>
int main(){
int i;
static int count;
for(i=NULL;i<=5;){
count++;
i+=2;
}
printf("%d",count);
return 0;
}
Output: 3
#include <stdio.h>
int main(){
#ifndef NULL
#define NULL 5
#endif
printf("%d",NULL+sizeof(NULL));
return 0;
}
Output: 2
Explanation:
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;
}
Output: (null)
Example:
void *ptr;
Here ptr is generic pointer.
int main(){
void *ptr;
printf("%d",*ptr);
return 0;
}
#include <string.h>
#include<stdio.h>
int main(){
void *ptr;
printf("%d",sizeof(ptr));
return 0;
}
Output: 2
Explanation: Size of any type of near pointer in c is two byte.
3. Generic pointer can hold any type of pointers like char pointer, struct
pointer, array of pointer etc without any typecasting.
Example:
#include<stdio.h>
int 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;
}
Output: A4
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.
Examples:
1.
char *c;
c=(char *)malloc(sizeof(char));
2.
double *d;
d=(double *)malloc(sizeof(double));
3.
Struct student{
char *name;
int roll;
};
Struct student *stu;
Stu=(struct student *)malloc(sizeof(struct student));
Wild pointer in c programming language.
Wild pointer:
A pointer in c which has not been initialized is known as wild pointer.
Example:
int main(){
int *ptr;
printf("%u\n",ptr);
printf("%d",*ptr);
return 0;
}
Output:
Any address
Garbage value
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.
3. File pointer:
To know about FILE pointer click here.
Later:
For example:
(q)What will be output of following c program?
#include<stdio.h>
int *call();
void main(){
int *ptr;
ptr=call();
fflush(stdin);
printf("%d",*ptr);
}
int * call(){
int x=25;
++x;
return &x;
}
#include<stdio.h>
int *call();
void main(){
int *ptr;
ptr=call();
fflush(stdin);
printf("%d",*ptr);
}
int * call(){
static int x=25;
++x;
return &x;
}
Output: 26
http://c-pointer.blogspot.com/2009/06/dangling-pointer-problem-in-c.html
In TURBO C there are three types of pointers. TURBO C works under DOS
operating system which is based on 8085 microprocessor.
1. Near pointer
2. Far pointer
3. Huge pointer
Near pointer:
The pointer which can points only 64KB data segment or segment number
8 is known as near pointer.
(If you don’t know what is data segment the click here)
That is 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.
Examples:
(1)
#include<stdio.h>
int main(){
int x=25;
int near* ptr;
ptr=&x;
printf(“%d”,sizeof ptr);
return 0;
}
Output: 2
(2)
#include<stdio.h>
int main(){
Output: 2 2
Explanation: Size of any type of near pointer is two byte.
Near pointer only hold 16 bit offset address. Offset address varies from
0000 to FFFF (in hexadecimal).
Example:
#include<stdio.h>
int main(){
int i=10;
int *ptr=&i;
printf("%p",ptr);
return 0;
}
Example:
#include<stdio.h>
int main(){
int a=12;
printf("%p",a);
return 0;
}
Output: 000C
Explanation: Hexadecimal value of 12 is C.
(1)
#include<stdio.h>
int main(){
Output: 0003
(2)
#include<stdio.h>
int main(){
int i;
char near *ptr=(char *)0xFFFA;
for(i=0;i<=10;i++){
printf("%p \n",ptr);
ptr++;
}
return 0;
}
Output:
FFFA
FFFB
FFFC
FFFD
FFFE
FFFF
0000
0001
0002
0003
0004
Explanation: When we increment or decrement the offset address from
maximum and minimum value respectively then it repeats the same value
in cyclic order. This property is known as cyclic nature of offset address.
What is default type of pointer in C?
Answer: It depends upon memory model.
What is memory model in C?
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.
Far pointer:
Examples:
int main(){
int x=10;
int far *ptr;
ptr=&x;
printf("%d",sizeof ptr);
return 0;
}
Output: 4
int main(){
Output: 4 2
Explanation: ptr is far pointer while *ptr is near pointer.
int main(){
int main(){
int x=100;
int far *ptr;
ptr=&x;
printf("%Fp",ptr);
return 0;
}
Output: 8FD8:FFF4
Note: %Fp is used for print offset and segment address of pointer in printf
function in hexadecimal number format.
In the header file dos.h there are three macro functions to get the offset
address and segment address from far pointer and vice versa.
Examples:
#include <dos.h>
#include<stdio.h>
int main(){
int i=25;
int far*ptr=&i;
printf("%X %X",FP_SEG(ptr),FP_OFF(ptr));
return 0;
}
#include <dos.h>
#include<stdio.h>
int main(){
int i=25;
int far*ptr=&i;
unsigned int s,o;
s=FP_SEG(ptr);
o=FP_OFF(ptr);
printf("%Fp",MK_FP(s,o));
return 0;
}
Output: 8FD9:FFF4 (Assume)
Note: We cannot guess what will be offset address, segment address and
far address of any far pointer .These address are decided by operating
system.
Example:
int i;
char far *ptr=(char *)0xB800FFFA;
for(i=0;i<=10;i++){
printf("%Fp \n",ptr);
ptr++;
}
return 0;
}
Output:
B800:FFFA
B800:FFFB
B800:FFFC
B800:FFFD
B800:FFFE
B800:FFFF
B800:0000
B800:0001
B800:0002
B800:0003
B800:0004
This property of far pointer is called cyclic nature of far pointer within same
segment.
1. Far pointer compares both offset address and segment address with
relational operators.
Examples:
int main(){
if(p==q)
printf("Both pointers are equal");
else
printf("Both pointers are not equal");
return 0;
}
int main(){
if(x==y)
printf("Both pointer are equal");
else
printf("Both pointer are not equal");
return 0;
}
Example:
(q)What will be output of following c program?
#include <dos.h>
#include<stdio.h>
int main(){
int j;
union REGS i,o;
char far *ptr=(char *)0XA0000000;
i.h.ah=0;
i.h.al=0x13;
int86(0x10,&i,&o);
for(j=1;j<=100;j++){
*(ptr+j)=4;
}
return 0;
}
Output: One red color line in the graphics console as shown in the following
figure
In the above program what is following codes?
union REGS i,o;
i.h.ah=0;
i.h.al=0x13;
int86(0x10,&i,&o);
Answer:
The task of this code is to switch the 256 bit color graphics console window
from default console window of Turbo c++ IDE.
More examples:
Copy the following code and execute the code in Turbo c compile and see
what is displaying.
int main(){
int j,k;
union REGS i,o;
char far *ptr=(char *)0XA0000000;
i.h.ah=0;
i.h.al=0x13;
int86(0x10,&i,&o);
for(k=0;k<100;k++){
for(j=1;j<=319;j++){
*(ptr+j+k*320)=k;
}
}
return 0;
}
#include <dos.h>
#include<stdio.h>
int main(){
int j,k;
union REGS i,o;
char far *ptr=(char *)0XA0000000;
i.h.ah=0;
i.h.al=0x13;
int86(0x10,&i,&o);
for(k=0;k<100;k++){
for(j=1;j<=319;j++){
*(ptr+j+k*320)=k;
}
}
return 0;
}
(a) Text byte: First byte stores character information. It stores character as
ASCII code.
(b) Color byte: Second byte stores color information of text byte character.
In other word we can say each even byte stores character and each odd
byte stores color.
Simple example:
int main(){
int i;
char far *ptr=(char *)0XB8000000;
*ptr='A';
*(ptr+1)=4;
return 0;
}
Output: It will display character A in the red color as shown following screen
dump:
Color scheme:
Color byte of size 8 bit stores the color information in the following manner.
Examples:
int main(){
int i;
char far *ptr=(char *)0XB8000000;
*ptr='A';
*(ptr+1)=1;
*(ptr+2)='B';
*(ptr+3)=2;
*(ptr+4)='C';
*(ptr+5)=4;
return 0;
}
Output:
#include<stdio.h>
int main(){
int i;
char far *ptr=(char *)0XB8000000;
*ptr='W';
*(ptr+1)=1;
*(ptr+2)='O';
*(ptr+3)=2;
*(ptr+4)='R';
*(ptr+5)=4;
*(ptr+6)='L';
*(ptr+7)=1;
*(ptr+8)='D';
*(ptr+9)=2;
return 0;
}
Output:
*ptr='W';
*(ptr+1)=3;
*(ptr+2)='O';
*(ptr+3)=5;
*(ptr+4)='R';
*(ptr+5)=6;
*(ptr+6)='L';
*(ptr+7)=7;
*(ptr+8)='D';
*(ptr+9)=3;
return 0;
}
Output:
(4)What will be output of following c program?
#include<stdio.h>
int i;
char far *ptr=(char *)0XB8000000;
*ptr='P';
*(ptr+1)=1+8;
*(ptr+2)='O';
*(ptr+3)=2+8;
*(ptr+4)='I';
*(ptr+5)=3+8;
*(ptr+6)='N';
*(ptr+7)=4+8;
*(ptr+8)='T';
*(ptr+9)=5+8;
*(ptr+10)='E';
*(ptr+11)=6+8;
*(ptr+12)='R';
*(ptr+13)=7+8;
return 0;
}
Output:
#include<stdio.h>
int i;
char far *ptr=(char *)0XB8000000;
*ptr='M';
*(ptr+1)=4+32;
*(ptr+2)='A';
*(ptr+3)=4+32;
*(ptr+4)='N';
*(ptr+5)=4+32;
*(ptr+6)='I';
*(ptr+7)=4+16;
*(ptr+8)='S';
*(ptr+9)=4+16;
*(ptr+10)='H';
*(ptr+11)=4+16;
return 0;
}
Output:
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.
Size of huge pointer is 4 byte or 32 bit.
Output: 4 4 1
Explanation: p is huge pointer, *p is far pointer and **p is char type data
variable.
Example:
(q) What will be physical address of huge address 0X59994444?
Answer:
When any relation operation is performed between two huge pointers first it
normalizes in actual physical address.
Example:
if(p==q)
printf("Equql");
else
printf("Not equal");
return 0;
}
Output: Equal
Explanation:
As we know huge pointers compare its physical address.
Output: 4 2 8
Explanation: q is far pointer, p is near pointer, *p is double data type
constant.
Memory model:
In c there are six type of memory model.
If you want to see all memory model in Turbo C++ IDE then open Turbo
C++ IDE and the go:
Options menu -> Compiler -> Code generation
These memory models are:
(a) TINY
(b) SMALL
(c) MEDIUM
(d) COMPACT
(e) LARGE
(f) HUGE
Properties of memory mode in C:
(1) Memory model decides the default type of pointer in C.
Note:
Examples:
int main(){
int *ptr;
printf("%d",sizeof ptr);
return 0;
}
int main(){
char (*fun)();
printf("%d",sizeof fun);
return 0;
}
int main(){
int near *p,*q;
printf("%d , %d",sizeof(p),sizeof(q));
return 0;
}
int main(){
char huge **p;
printf("%d , %d",sizeof(p),sizeof(*p));
return 0;
}
int main(){
#if defined __TINY__
printf("Memory model is: TINY");
#elif defined __SMALL__
printf("Memory model is:SMALL ");
#elif defined __MEDIUM__
printf("Memory model is:MEDIUM ");
#elif defined __COMPACT__
printf("Memory model is:COMPACT ");
#elif defined __LARGE__
printf("Memory model is:LARGE ");
#elif defined __HUGE__
printf("Memory model is:HUGE ");
#endif
return 0;