C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 4)
4.
What is the output of the program in Turbo C (in DOS 16-bit OS)?
#include<stdio.h>
int main()
{
    char *s1;
    char far *s2;
    char huge *s3;
    printf("%d, %d, %d\n", sizeof(s1), sizeof(s2), sizeof(s3));
    return 0;
}
2, 4, 6
4, 4, 2
2, 4, 4
2, 2, 2
Answer: Option
Explanation:

Any pointer size is 2 bytes. (only 16-bit offset)
So, char *s1 = 2 bytes.
So, char far *s2; = 4 bytes.
So, char huge *s3; = 4 bytes.
A far, huge pointer has two parts: a 16-bit segment value and a 16-bit offset value.

Since C is a compiler dependent language, it may give different output in other platforms. The above program works fine in Windows (TurboC), but error in Linux (GCC Compiler).

Discussion:
55 comments Page 6 of 6.

Amol said:   6 years ago
@Sirji if you used * as like above example then you got an error.

Gourvi said:   5 years ago
@For_all.

Here in this question, we have 3 type of pointers that is near, far and huge. And by default in c we have near pointer, for eg: int *p, here size of (p) would be 2 bytes in 16 bit complier. But here far and huge pointers are also being used and there sizeof is 4 bytes.

P.S: Forget about char size as here main focus is on near, far and huge pointers.

And for more clear understanding please refer google.
(2)

Dipak said:   5 years ago
No, it's wrong because every pointer has same size irrespective to its base type so its either 4 bytes or 8 bytes depend upon the compiler 32 bit or 64 bit irrespectively.

Shri Hari Rajeswar said:   5 years ago
Can you please explain how you said like this? because pointer holds same bytes irrespective of the base you use.

Sanjana said:   2 years ago
1. Near Pointer:
The Near Pointer is used to store the 16-bit addresses. It means that they can only reach the memory addresses within the current segment on a 16-bit machine. That is why we can only access the first 64 kb of data using near-pointers.
The size of the near pointer is 2 bytes.

2. Far Pointer:
A far pointer stores the address in two 16-bit registers that allow it to access the memory outside of the current segment. The compiler allocates a segment register to store the segment address, and then another register to store offset within the current segment. The offset is then added to the shifted segment address to get the actual address
The size of the far pointer is 4 bytes.

3. Huge Pointer:
The huge pointer also stores the addresses in two separate registers similar to the far pointer. It has the following characteristics:
In the Huge pointer, both offset and segment address is changed.
The Huge Pointers always compare the absolute addresses, so the relational operation can be performed on it.
The size of the huge pointer is 4 bytes.
(1)


Post your comments here:

Your comments will be displayed after verification.