What is Pointers

(1) What is pointer ?
Ans:
Pointer is variable which contain address of another variable.
e.g
1.
int i=3;
meaning:-


6024 any arbitrary address.
2.
int i=3;
int *j;
j=&i;
meaning:
Here j is pointer it contain 6024 which is address of another variable i.
3.
int i=3;
int *j;
int **k;
j=&i; //line 1
k=&j; //line 2

Meaning:
Here both i and j are pointer where j contain 6024 which is address of variable I while j contain 8085 which is address of another variable which contain address of variable i.
Now

printf(“%d”, **k);
printf(“%d”,*j);
printf(“%d”,i);


Above all printf will give same output : 3
Explanation:
* and & are canceled to each other i.e *&a=a
So **k
= **(&j) //from line 1
= *(*&j)
= *j //* and & are canceled to each other.
= *(&i) //from line 2
= *&i
= i
Thus **k = i ,hence **k is giving output 3

Same way *j = i
(2) What will output:
void main()
{
int i=3;
int *j;
int **k;
j=&i;
k=&j;
printf(“%u %u %u”,i,j,k);
}
Ans:


Here 6024 ,8085,9091 is any arbitrary address,it may be different.
Since content of i is 3,so first output will 3
Content of j is 6024 ,so second output will 6024
Content of k is 8085 ,so third output will 9091
Output: 3 6024 8085
Note:-Address is always a whole number ,which can not be negative so we generally is %u instead of %d

Memory model

(2)How many pointers are in turbo C?
Ans:
3
1. Near pointer (16 bit)
2. Far pointer (32 bit)
3. Huge pointer (32 bit)
(3) How many memory models?
Ans:

According to size of program in code area,data area and stack area there are six type of memory model:
1. Tiny
2. Small (default)
3. Medium
4. Compact
5. Large
6. Huge

Note: - to change memory model (in turbo c) go to
Option->compiler->code generation.
(1) What will output :

Void main()
{
int * p,b;
b=sizeof(p);
printf(“%d”,b);
}
Ans:
Output: 2 or 4
Explanation:
Since in this question it has not written p is which type pointer.So it’s output will depends upon which memory model has selected. Default memory model is small.
Memory model...............................................default pointer size

Tiny, Small (default), Medium near...................................2
Compact,Large,Huge far......................................................4

Near pointer

(1) near pointer?
Ans:
It is 16 bit pointer.It can hold the address of variable only within 64KB data segment.It store only offset address of any variable.This offset address in cyclic in nature .








If you will increment the near pointer then offset address will reach maximum value FFFF (in hexadecimal) then 0000 and so on.
For example :
#include
void main()
{

int a=5,i;
int *ptr;
//by default it is near pointer.
ptr=&a;
for(i=0;i<300;i++)

{

printf(“\n %p”,ptr);

p++;

delay(100);

}

}

Output :

Here int two byte data type so every time offset address is increasing by two. %p gives offset address in hexadecimal number. We can perform ++,-- ,+,- relation operator (>,<,==,….) operation on offset address. We cannot perform following task. Addition of two offset address. Multiplication and division of two offset address or one offset address and another number.


Far Pointers

(1) What is far pointer?
Ans:
Far pointer is 32 bit pointer .It can hold the address of variable in the data segment as well as outside of the data segment.

This type modifier is usually used when compiling in the tiny, small or compact memory model to force the pointer to be far. It stores both offset address as well as segment number of any variable But when you will increment far address then only offset address will increase (same way as in near pointer).Its segment number never will change. In dos.h header file there two function that can find offset address and segment address which take the argument far pointer and return unsigned int.
Syntax:
unsigned int FP_OFF(void far *)
unsigned int FP_SEG(void far *)
e.g
#include
void main()
{
unsigned int i,offset_addr,seg_addr,;
char far *ptr=(char far *) 5555 FF99;
for (i=0;i<300;i++)
{
seg_addr=FP_SEG(p);
offset_addr=FP_OFF(p);
printf(“\n seg_addr %x offset_addr %x ”,seg_addr,offset_addr);
p++;
delay(100);
}
}


Output :


You can see its segment address is not changing. In relational operation whole far address is compared.

Physical Address Calculation

(13)How we can calculate actual memory address if we know segment and offset address ?
Ans:
Physical address =segment address * 16 + offset address; (in decimal)
Physical address =segment address * 0x10 + offset address; (in hexadecimal)

e.g
Let far address is 0x59994444 (in c hexadecimal integer constant has prefix 0x);
offset address is 0x4444
segment address is 0x5999
temp=segment address *0x10
=0x59990 (trick : only write one 0 to the right side )
=0x9990 (since offset address can have only for hexadecimal digit ,so remove the last digit)
Physical address =temp+offset address
=0x9990+0x4444
=0x0DDD4 (physical address are 20 bit so it is represented in 5 hexadecimal digit)
Process of conversion of 32 bit far address into 20 bit physical address is called normalization.

Note: It is possible that two different far address can represent same physical address


Huge Pointers

(13)What is huge pointer ?
Ans :

Is is 32 bit pointer .Its is similar to far pointer but any arithmetic or relational operation is performed in huge address then first it is normalize.
What will be output:
void main()
{
int huge *a=(int huge *)0x59990005;
int huge *b=(int huge *)0x59980015;
if(a==b)
printf("power of pointer");
else
printf("power of c");
getch();
}
Output: power of pointer
Explanation:
Here we are performing relational operation between two huge address. So first both a and b will normalize.
a=(0x5999)* (0x10)+(0x0005)=0x9990+0x0005=0x9995
b=(0x5998)* (0x10)+(0x0015)=0x9980+0x0015=0x9995
Here both huge address is representing same physical address. So a==b is true.
Note. Two 32 bit address can be represent same physical address but two physical address must be unique.


How will we Read Complex pointers

Rule 1. Give the first priority to the name (identifier) of pointer variable.
Rule 2. Give the least priority of to the data type with modifier (like int,char,float,unsigned int,static int etc.)
Rule 3. () and [] operator has higher priority (associativity left to right ) than * ,& (associativity right to left)
Priority: It means operator which have highest priority will execute first.
Associativity: If associativity is left to right and two operator have same priority then left most operator will have more priority and vice versa.
(to read the priority and associativity of each operator click here)
How to read
If right side of name (identifier) is ( ) then read function after the function read all as return type
Read [ ] operator array and after this read as it contain
Read * pointer if you have not encounter function or array otherwise read address.
If you will read example then you will understand more easily.

Example 1.
Read the pointer
int (*ptr)();

Ans:
Give first priority to ptr :1

There are two () operator ,associativity of () operator is left to right so left most operator will have more priority.

Left most ( ) operator will execute first . So give * to the second priority and right most ( ) operator to the third priority.
Give fourth priority to int

Read : ptr is pointer to such function which return type is integer data type.
Program:
#include
#include
void main()
{
int (*ptr1)();
int (*ptr2)();
void (*ptr3)();
ptr1=puts;
ptr2=getch;
ptr3=clrscr;
(*ptr3)();
(*ptr1)("POWER OF POINTER");
(*ptr2)();
Output: POWER OF POINTER
Example 2.
char *arr [3];
Ans :

arr is array of size 3 which contain address of char
program:
void main()
{
char *arr[3];
char a=1,b=2,c=3;
arr[0]=&a;
arr[1]=&b;
arr[2]=&c;
clrscr();
printf("%u %u",&b,arr[1]);
printf("\n%d",*arr[2]);
getch();
}
Output :
any addresss same address
3

Example 3
char (*ptr)[5];

Ans:

ptr is pointer to array of size 5 which contain char data type.
Program:
#include
#include
void main()
{
char arr[5]={1,2,3,4,5};
char (*ptr)[5];
ptr=&arr;
ptr=(*ptr)+2;
clrscr();
printf("%d",**ptr);
getch();
}
Output: 3
Example 4
unsigned long int ( * avg ())[ 3]
Program:

Ans:

avg is such function which return type is address of array of size of 3 which contain unsigned long int data type.
Program:
#include
#include
unsigned long int (* avg())[3]
{
static unsigned long int arr[3]={1,2,3};
return &arr;
}
void main()
{
unsigned long int (*ptr)[3];
ptr=avg();
clrscr();
printf("%d",*(*ptr+2));
getch();
}

0 comments:

Followers

 
C - Programming -