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.

Ashwini said:   9 years ago
Can't understand it. Explain it more detail.

Arusha said:   9 years ago
Can't understand it. Explain.

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

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.

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

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.

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?

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

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.

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


Post your comments here:

Your comments will be displayed after verification.