C Programming - Complicated Declarations - Discussion


What will be the output of the program?


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;

[A]. 4, 4, 4
[B]. 2, 2, 2
[C]. 2, 8, 4
[D]. 2, 4, 8

Answer: Option A


No answer description available for this question.

Arun said: (Aug 30, 2010)  
Confused about these near, far, huge pointers.

Can anyonre explain this concept?.

Khattu said: (Oct 19, 2010)  
near, far, huge these are deprecated.

Vijay said: (Oct 24, 2010)  
Near pointer : If the size of the program is very large the program may create a near pointer to a piece of frequently accessed memory so as to enhance performance.

But I am curious to know that so the near pointer is allocated/ created by the system rather than the user.

Far pointer: If the program requires a lot of data, then a separate space is allocated for it outside the program data segment. So then a far pointer is used to access these memory locations so that the speed remains fast.

Huge pointer: In an 8086 family of processors, the max. Size of one data item can be 64k. But to override this default setup we can make use of huge pointers to have an object of size larger than 64k.

Satish Birajdar said: (Jan 7, 2011)  
far pointer refers to other code segment
near pointer refers to same code segment

Sowmya said: (Jun 30, 2011)  
Its a real good reference.

Goaku said: (Aug 12, 2011)  
Can we leave such blank spaces while declearing variables?

Kiran said: (Nov 13, 2011)  
Huge *far *near *ptr1---> is this a single variable?

If it is a single variable we should not use spaces in b/w the variables?

Can any one give me the explination please?

Thamil said: (Dec 30, 2011)  
Need proper clarification regarding this problem.

Bharath said: (Jan 11, 2012)  
Please any one explain about this problem I did't get properly.

Vikas said: (Feb 19, 2012)  
Thanks sowmy its really a good reference.

Shubham Agnihotri said: (Feb 19, 2012)  
char huge *near *far *ptr1;
here *ptr1 is huge pointer
here **ptr1 is near pointer
here ***ptr1 is far pointer.

Nw take it similarly in al.l cases & the o/p is justified .

Purnima Prasad said: (Mar 19, 2012)  
char huge*near*far*ptr1;
here *ptr1 is near pointer
**ptr1 is huge pointer
and then output will be justified.

Shashi said: (Jul 30, 2012)  
Treat expression as pointer pointing to pointer.

E.g. char huge*near*far*ptr1 means ptr1 points to far type (or say address of far type) ; far is again pointing to near type and near to huge.

Thus ptr--> 4 bytes (far)
*ptr-->2 bytes (near)
**ptr-->4 bytes (huge)

This happened becausse associativity of pointer is right to left.

Nagarjuna said: (Aug 4, 2012)  
Size of the pointer will take the size is 4 bytes.

Abhijit_Softlove said: (Oct 9, 2012)  
char x *y *z *ptr; / x y &z near huge or far

sizeof(ptr)= sizeof(z)
sizeof(*ptr)= sizeof(y)
sizeof(**ptr)= sizeof(x)

Srijan said: (Jan 3, 2013)  
Whats the size of near, huge and far pointer ?

Praveen said: (Mar 13, 2013)  
size of near pointer is 2 bytes;
sizeof huge pointer is 4 bytes;
sizeof far pointer is 4 bytes;

Jaswanth said: (Dec 14, 2013)  
Any pointer holds 4 bytes of memory in gcc compiler.

Naseeb said: (Feb 12, 2014)  
What is GCC compiler?

Sumasree said: (May 12, 2014)  
It's supports multiple languages and also portable runs on all available platforms and free s/w, but turbo C don't have these all.

Pavan said: (Oct 28, 2014)  
Exp: far, near and huge pointer are used in dos only. Because there is only one mb memory for accessing. 1mb memory is divided into segment.

There are various segment like cs(code segment), ss(stack segment), ds(data segment) and more like extra segment etc.

Such type of pointer are used to access the memory.

Meerb said: (Apr 22, 2015)  
Please me the answer of this question please.

What will be the size of a pointer, pointing to an object of size 1MB?

Riya said: (Sep 23, 2015)  
If near pointer is 2 byte then why the output is 4 byte for ptr 2.

Jayshree said: (Oct 3, 2015)  
I am confused about output can you explain anyone again!

Rohith said: (Feb 27, 2016)  
Hey guys lets see this in a simple way. As C program is platform dependent first it goes to far which is inside near and as far will take 4 bytes it will take four bytes and last inside near there is far near is 2 bytes but is 4 bytes so it will take 4 bytes.

Monika said: (Jun 10, 2016)  
The %d is used for integers so it will show the size of int.

Pranali said: (Apr 1, 2017)  

Nope, if we write as,

int *p;

it will shows size of '*'i.e. pointer not of 'int'.

Sun Yijia said: (Mar 12, 2018)  
There are only there pointers(ptr1,ptr2,ptr3), we can regard them to 3 level pointers.

Example ptr1----> char * * * ptr1,------> char huge* * * ptr1 ------> char huge* near* *ptr1-----> char huge* near* far* ptr1.

Every level represents different pointer types.

So , there is the same answer, **ptr1/ptr2/*ptr3 are the huge pointer type.

Parth Barde said: (Sep 21, 2018)  
Thank you all for explaining the solution.

Mikkawy said: (Sep 15, 2020)  

GCC (Gnu Compiler Collection).

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.