Showing posts with label Pointers. Show all posts
Showing posts with label Pointers. Show all posts

Thursday

wild,dagling,near,far and huge pointer

0 comments
 Wild pointer in c programming language.

  Wild pointer:

           A pointer in c which has not been initialized is known as wild pointer.

    Example:

   What will be output of following c program?

            #include<stdio.h>

             int main(){

              int *ptr;

              printf("%u\n",ptr);

              printf("%d",*ptr);

               return 0;

           }

         Output:

            Any address

            Garbage value

        Here ptr is wild pointer because it has not been initialized.

     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 problem in c programming

Different types of pointers:

      1. Dangling pointer:

        If any pointer is pointing the memory address of any variable but after some variable has deleted           from that memory location while pointer is still pointing such memory location. Such pointer is known as   dangling pointer and this problem is known as dangling pointer problem.

   Initially:

   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;

        }

Output: Garbage value

Note: In some compiler you may get warning message returning address of local variable or temporary

Explanation: variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.

Solution of this problem: Make the variable x is as static variable.

In other word we can say a pointer whose pointing object has been deleted is called dangling pointer.

               #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

Near pointer in C programming

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(){

int near* near * ptr;

printf(“%d”,sizeof(ptr),sizeof(*ptr));

return 0;

}

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:

#include<stdio.h>

int main(){

int i=10;

int *ptr=&i;

printf("%p",ptr);

return 0;

}

Output: Offset address in hexadecimal number format.

%p is also used to print any number in hexadecimal number format.

Example:

#include<stdio.h>

int main(){

int a=12;

printf("%p",a);

return 0;

}

Output: 000C

Explanation: Hexadecimal value of 12 is C.

Consider the following two c program and analyze its output:

(1)

#include<stdio.h>

int main(){

int near * ptr=( int *)0XFFFF;

ptr++;

ptr++;

printf(“%p”,ptr);

return 0;

}

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.

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.

What is memory model in C?

Far pointer in c programming

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:

(If you don’t know what is segment the click here)

Size of far pointer is 4 byte or 32 bit.

Examples:

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

#include<stdio.h>

int main(){

int x=10;

int far *ptr;

ptr=&x;

printf("%d",sizeof ptr);

return 0;

}

Output: 4

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

#include<stdio.h>

int main(){

int far *near*ptr;

printf("%d %d",sizeof(ptr) ,sizeof(*ptr));

return 0;

}

Output: 4 2

Explanation: ptr is far pointer while *ptr is near pointer.

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

#include<stdio.h>

int main(){

int far *p,far *q;

printf("%d %d",sizeof(p) ,sizeof(q));

return 0;

}

Output: 4 4

First 16 bit stores: Segment number

Next 16 bit stores: Offset address

What is segment number and offset address?

Example:

#include<stdio.h>

int main(){

int x=100;

int far *ptr;

ptr=&x;

printf("%Fp",ptr);

return 0;

}

Output: 8FD8:FFF4

Here 8FD8 is segment address and FFF4 is offset address in hexadecimal number format.

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.

1. FP_OFF(): To get offset address from far address.

2. FP_SEG(): To get segment address from far address.

3. MK_FP(): To make far address from segment and offset address.

Examples:

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

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

}

Output: Any segment and offset address in hexadecimal number format respectively.

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

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

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.

Example:

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

#include<stdio.h>

int main(){

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.

Important points about far pointer:

1. Far pointer compares both offset address and segment address with relational operators.

Examples:

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

#include<stdio.h>

int main(){

int far *p=(int *)0X70230000;

int far *q=(int *)0XB0210000;

if(p==q)

printf("Both pointers are equal");

else

printf("Both pointers are not equal");

    return 0;

}

Output: Both pointers are not equal

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

#include<stdio.h>

int main(){

int far *p=(int *)0X70230000;

int far *q=(int *)0XB0210000;

int near *x,near*y;

x=(int near *)p;

y=(int near *)q;

if(x==y)

printf("Both pointer are equal");

else

printf("Both pointer are not equal");

   

    return 0;

}

Output: Both pointers are equal

2. Far pointer doesn’t normalize.

What is normalization of pointer?

Huge pointer in c programming

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.

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

#include<stdio.h>

int main(){

char huge * far *p;

printf("%d %d %d",sizeof(p),sizeof(*p),sizeof(**p));

return 0;

}

Output: 4 4 1

Explanation: p is huge pointer, *p is far pointer and **p is char type data variable.

Normalization of huge pointer:

Turbo C compiler is based on 8085 microprocessor in which physical address of memory is represented in 20 bit. Conversion of 4 byte or 32 bit huge address into 20 bit actual physical address is known as normalization.

Formula to calculate physical address:

Example:

(q) What will be physical address of huge address 0X59994444?

Answer:

Huge address: 0X59994444

Offset address: 0x4444

Segment address: 0x5999

Physical address= Segment address * 0X10 + Offset address

=0X5999 * 0X10 +0X4444

=0X59990 + 0X4444

=0X5DDD4

In binary: 0101 1101 1101 1101 0100

Note: Each hexadecimal digit is represented in 4 bit binary number.

When any relation operation is performed between two huge pointers first it normalizes in actual physical address.

Example:

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

#include<stdio.h>

int main(){

   int huge*p=(int huge*)0XC0563331;

   int huge*q=(int huge*)0xC2551341;

   if(p==q)

     printf("Equql");

   else

     printf("Not equal");

   return 0;

}

Output: Equal

Explanation:

As we know huge pointers compare its physical address.

Physical address of huge pointer p

Huge address: 0XC0563331

Offset address: 0x3331

Segment address: 0XC056

Physical address= Segment address * 0X10 + Offset address

=0XC056 * 0X10 +0X3331

=0XC0560 + 0X3331

=0XC3891

Physical address of huge pointer q

Huge address: 0XC2551341

Offset address: 0x1341

Segment address: 0XC255

Physical address= Segment address * 0X10 + Offset address

=0XC255 * 0X10 +0X1341

=0XC2550 + 0X1341

=0XC3891

Since both huge pointers p and q are pointing same physical address so if condition will true.

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

#include<stdio.h>

int main(){

double near *p,far *q;

printf("%d %d %d",sizeof(q),sizeof(p),sizeof(*p));

return 0;

}

Output: 4 2 8

Explanation: q is far pointer, p is near pointer, *p is double data type constant.

Don’t use huge pointer:

If you will increment huge pointer it will increment both offset and segment address unlike to far pointer which only increments offset address. So if you have little knowledge about huge pointer and you are using huge pointer then you can easily access and modify the IVT, device driver memory, video memory etc. This might be dangerous for your computer.

(q)Why there are three types of pointer in Turbo c compiler?

Answer:

Turbo c compiler is based on Dos operating system which is based on 8085 microprocessors. In 8085 microprocessor actual physical address is represented in 20 bit. But there are not any pointers which can point 20 bit address. Considering simplicity of calculations, access to actual physical address, security etc. c has introduced three type of pointer i.e. near, far and huge pointer.
Read More ->>

Pointer to array of character,array of integer&array of array in c

0 comments

Pointer to array of character in c

Pointer to array of character: A pointer to such an array which contents is character constants is known as pointer to array of character constant.

What will be output if you will execute following code?


#include<stdio.h>
char display(char (*)[]);

int main(){

char c;
char character[]={65,66,67,68};
char (*ptr)[]=&character;

c=display(ptr);
printf("%c",c);


return 0;
}

char display(char (*s)[]){
**s+=2;
return **s;
}

Output: C
Explanation: Here function display is passing pointer to array of characters and returning char data type.

**s+=2
=>**s=**s+2
=>**ptr=**ptr+2 //s=ptr
=>**&character= **&character+2 //ptr=&character
=>*character=*character+2 //from rule *&p =p
=>character[0]=character[0]+2 //from rule *(p+i)=p[i]
=>character [0] =67
**s=character [0] =67

Note: ASCII value of ‘C’ is 67

Pointer to array of integer in c




Pointer to array of integers: A pointer to such an array which contents are integer numbers is known as pointer to array of integer.


What will be output if you will execute following code?


#include<stdio.h>

int main(){

static int i,j,k;
int *(*ptr)[];
int *array[3]={&i,&j,&k};

ptr=&array;
j=i+++k+10;
++(**ptr);

printf("%d",***ptr);


return 0;
}

Output: 10
Explanation:

In this example:

array []: It is array of size three and its content are address of integer.

ptr: It is pointer to array which content are address of integer.

Pictorial representation above declaration:







Note: In the above figure upper part of box represent content and lower part represent memory address. We have assumed arbitrary address.

j=i+++k+10
=i++ + k+10
=0 +0 +10=10

***ptr = *** (&array) //ptr=&array
= **array //From rule *&p=p
//From rule array [0] =*(array+0) and ++ (**ptr)
=*array [1]
=*&j
=j
=10



What will be output if you will execute following code?


#include<stdio.h>

int main(){

int i,j,k;
int *(*ptr)[];
int *array[3]={&i,&j,&k};

ptr=&array;
j=i+++k+10;
++(**ptr);

printf("%d",***ptr);


return 0;
}

Output: Compiler error
Explanation: Address of auto variable cannot be member of an array.

Pointer to array of array in c



Examples of pointer to array of array in c:


What will be output if you will execute following code?


#include<stdio.h>
int 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;
}

Output: 5.000000
Explanation:

In this example:

farray [][3]: It is two dimension array and its content are float constants.

array [3]:It is one dimension array and its content are address of such one dimension array which content are float constant.

ptr: It is pointer to one dimension array which content are address of such one dimension array which content are float constant.

Pictorial representation:
Note: In the above figure upper part of box represent content and lower part represent memory address. We have assumed arbitrary address.

2[(*(**ptr+1))]
= (*(**ptr+1)) [2]
= (*(**&array+1)) [2]
= (*(*array+1)) [2]
= (*(array [0] +1)) [2]
= (*(&farray [0] +1)) [2]
=&farray [0] [1] [2]
=*&farray [1] [2]
=farray [1] [2]

It is 1*(3) +2=5th element of farray starting from zero which is 5.0f
Read More ->>

pointer to two,three dimensional array

0 comments

 

Pointer to two dimensional array in c programming

Examples of pointers to 2 dimensional array:


What will be output if you will execute following code?


#include<stdio.h>
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

 

Pointer to three dimensional array in c programming



Examples of pointers to 3 dimensional array:


#include<stdio.h>
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:
Read More ->>

Pointer to array of pointer to string in c programming

0 comments

Pointer to array of pointer to string in c programming



Pointer to array of pointer to string: A pointer to an array which contents are pointer to string.


Example of Pointer to array of pointer to string:


What will be output if you will execute following code?


#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;
}

Output: che
Explanation:

Here
ptr: is pointer to array of pointer to string.

P1, p2, p3: are pointers to array of string.

array[3]: is array which contain pointer 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”
Read More ->>

Pointer to structure in c programming

0 comments

Pointer to structure in c programming



Pointer to structure: A pointer which is pointing to a structure is know as pointer to structure. 


Examples of pointers to structure:

What will be output if you will execute following code?


#include<stdio.h>

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

Output: A.Kumar H-2
Explanation:

p is pointer to structure address.
-> and (*). Both are same thing. These operators are used to access data member of structure by using structure’s pointer.
Read More ->>
 

| C programing tutorials © 2009. All Rights Reserved | Template Style by My Blogger Tricks .com | Design by Brian Gardner | Back To Top |