C Programming - Complicated Declarations - Discussion

Discussion Forum : Complicated Declarations - Find Output of Program (Q.No. 4)
4.
What will be the output of the program (in Turbo C under DOS)?
#include<stdio.h>

int main()
{
    char huge *near *far *ptr1;
    char near *far *huge *ptr2;
    char far *huge *near *ptr3;
    printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));
    return 0;
}
4, 4, 8
2, 4, 4
4, 4, 2
2, 4, 8
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
32 comments Page 2 of 4.

Deepika said:   1 decade ago
All the varibles have same type ouput is 4,4,4 but output is 4,4,2

Gautam A Naik said:   1 decade ago
But why is sizeof (ptr3) will give output as 2?. please explain me.

Ravi said:   1 decade ago
Think it like this :
ptr1 is a far pointer to a near pointer to a huge pointer to char .
Think arrow (->) sign as "pointing to" :
ptr1 (far type) -> *ptr1 (near type) -> **ptr1(huge type) -> ***ptr1 (char type)
Now
sizeof(ptr1)= far pointer size = 4 bytes
sizeof(*ptr1)= near pointer size = 2 bytes
sizeof(**ptr1)= huge pointer size = 4 bytes

Adarsh said:   1 decade ago
Actually far and near are related to fact that how we are accessing the variable i.e. absolute or relative.

Relative = "near";.

Absolute="far".

When we are accessing variable relative, then we hav to store only relative address that account for our 2 bytes of memory.

And the rest is understood.

Kavi said:   1 decade ago
What is the purpose of near, huge and far pointers. Explain with an example program?

Hany said:   1 decade ago
Why we consider the size of far, huge and near as 4, 4 and 2 respectively. Then in declaration of first line also there is near so why it takes 4bytes of size?

Rampa said:   1 decade ago
There is something known as memory segment for near it saves relative memory and far and huge it saves absolute memory.

For a 32 bit machine memory bus width is 32 bit that means it can support maximum of 2^32 capacity that is 4Gb memory. In the sense to store address of any byte in absolute form we need 32 bit pointer (full address). But in near there will be something base pointer (internally) which stores 32 bit address and all other pointer which we declare stores only relative address that is 2 bytes. For example if base has 10000000h address and p=ffffh (where p is pointer) then absolute address of p is 10000000h+ffffh=1000ffffh. Where base address contains the starting address of a near segment.

Rohit said:   1 decade ago
#include<stdio.h>

int main()
{
char huge *near *far *ptr1;
printf("%d %d\n", sizeof(***ptr1),sizeof(****ptr1));
return 0;
}

Priyanka said:   1 decade ago
Irrespective of pointer it declares far is of size 4,

Huge is of size 4,

Near is of size 2.

Divya said:   9 years ago
Don't understand the concept of near far huge pointers. Help me by explaining this.


Post your comments here:

Your comments will be displayed after verification.