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 1 of 4.

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.

Sahithi said:   8 years ago
When working on architectures with segmented memory (like x86 real mode), one can distinguish three types of pointer addresses (examples for x86 in segment:offset notation):

Near:
Only stores the offset part (which is 16-bit) - when resolving such a pointer, the current data segment offset will be used as segment address.

Far:
Stores segment and offset address (16 bit each), thus defining an absolute physical address in memory.

Huge:
Same as far pointer, but can be normalized, i.e. 0000:FFFF + 1 will be wrapped around appropriately to the next segment address.

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.

Divya said:   6 years ago
Chat huge *near *far *ptr1;
Ptr1= far so 4// check from right to left.

char near *far *huge *ptr2;
*Ptr2= far so 4// one star so second is taken.
char far *huge *near *ptr3;
**Ptr3=far so 4.

Sai said:   1 decade ago
it will consider the *far *ptr1---------------- * far 4bytes
*huge *ptr2--------------- *huge 4 bytes
*near *ptr3 -----------------near 2 bytess

Somnath said:   1 decade ago
ptr1 is a pointer to a far pointer for that the size is 4
ptr2 is a pointer to a huge pointer for that the size is 4
ptr3 is a pointer to a near pointer for that the size is 2

Suresh@pdtr said:   1 decade ago
Dear guys,

It's easy to remember,
as we can say when ever after (huge,far,near) declaration of a pointer variable it takes default sizes of huge, near, far.!

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?

Kumar said:   1 decade ago
@NN

The above code works fine in Turbo C as said.

But in GCC it not works.

Is this due to platform dependency of C compiler?

Can you explain please?


Post your comments here:

Your comments will be displayed after verification.