Advance C


VERY INTESTING THING:


Video memory

Segment number 0XA and 0XB is known as video memory. There are two types of video memory.

(a) Text video memory
(b) Graphics video memory

(a) Text video memory:
Segment number 0XB is known as text video memory. This segment is divided into 25 rows and 80 columns which creates 80*25=2000 cells.
Size of each cell is two byte. Each cell is divided into two parts each of size one byte.

(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:

(q)What will be output of following c program?

void main(){
int i;
char far *ptr=(char *)0XB8000000;
*ptr='A';
*(ptr+1)=4;

}
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.


First four bits stores color information of character.

0000 0001: Blue color (1)
0000 0010: Green color (2)
0000 0100: Red color (4)
0000 1000: To increase the intensity of color. (8)

Note: Any other number will generate mixture of above four basic colors.

Next four bits stores color information of background of character.

0001 0000: Blue color (16)
0010 0000: Green color (32)
0100 0000: Red color (64)
1000 0000: To increase the intensity of color. (128)

Note: Any other number will generate after mixing of above four basic colors.

Examples:

(1)What will be output of following c program?

void 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;

}

Output:





(2)What will be output of following c program?


void 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;

}

Output:



(3)What will be output of following c program?
//Mixture of basic color

void main(){
int i;
char far *ptr=(char *)0XB8000000;
clrscr();
*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;

}

Output:



(4)What will be output of following c program?
//To increase the intensity of color.

void main(){
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;

}

Output:



(5)What will be output of following c program?
// for background color

void main(){
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;

}

Output:




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.


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)
void main(){
int x=25;
int near* ptr;
ptr=&x;
printf(“%d”,sizeof ptr);
}
Output: 2
(2)
void main(){
int near* near * ptr;
printf(“%d”,sizeof(ptr),sizeof(*ptr));
}
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).
Note: In printf statement to print the offset address in hexadecimal, %p is used.
Example:
void main(){
int i=10;
int *ptr=&i;
printf("%p",ptr);
}
Output: Offset address in hexadecimal number format.
%p is also used to print any number in hexadecimal number format.
Example:
void main(){
int a=12;
printf("%p",a);
}
Output: 000C
Explanation: Hexadecimal value of 12 is C.
Consider the following two c program and analyze its output:
(1)
void main(){
int near * ptr=( int *)0XFFFF;
ptr++;
ptr++;
printf(“%p”,ptr);
}
Output: 0003
(2)
void main(){
int i;
char near *ptr=(char *)0xFFFA;
for(i=0;i<=10;i++){
printf("%p \n",ptr);
ptr++;
}
}
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.
Cyclic property of offset address.
If you increment the near pointer variable then move clockwise direction. If you decrement the near pointer then move anti clockwise direction.

What is default type of pointer in C?
Answer: It depends upon memory model.