C Programming - Complicated Declarations - Discussion

1. 

What will be the output of the program?

#include<stdio.h>

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

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

Answer: Option C

Explanation:

No answer description available for this question.

Sathish said: (Dec 1, 2010)  
Any body please give what's the difference between these three declarations.

Hari said: (Dec 13, 2010)  
Near ptr has a memory of 2bytes. Far and huge ptr hav a memory of 4 bytes. Hence the output is 2, 4, 4.

Nishu said: (Dec 31, 2010)  
Thanks hari.

Prasad said: (Jan 11, 2011)  
Can anybody provide me link to know about these near and far pointers?

Yasir said: (Jan 18, 2011)  
Thanks dude.

Badhusa said: (Jan 21, 2011)  
Thanks ma.

Subash said: (Feb 13, 2011)  
Will any body give me the full bytes details of these type of declarations. ?

Shashidhar said: (Aug 16, 2011)  
Can any one say me the difference between these three pointers?

Sakshi Chopra said: (Sep 18, 2011)  
Near pointers-refer to current segment $ point to 64kbytes, occupy 2bytes.

Far pointer-refer to data segment $ points to gigabyte, occupy 4 byte.

Huge pointer-similar to far pointers, but work slowpy, occupy 4 byte.

Seshan said: (Oct 16, 2011)  
Thanks for your message sakshi chopra.

Monika said: (Nov 5, 2011)  
Thank shakshi.

Shankar said: (Jan 19, 2012)  
Thanks sakshi.

Vishal And Sandeep said: (Mar 9, 2012)  
Associativity in pointer is right to left... so for
char far *near *ptr1;(think it is like type casting or pointer to pointer).
ptr1 will contain address of (*near) which will be 2 byte and that contain the address of far memory character so it will now 4 byte.
so the sizeof(ptr1)= 2 byte and sizeof(*ptr)= 4 byte.

Mahesh Babu said: (Sep 2, 2012)  
Sakshi Chopra thanks for the answer Sir.

Satish Bhati said: (Feb 26, 2015)  
GNU GCC v 4.8 compiler is showing error.

And GCC Compiler (32 Bit Linux Platform) of IndiaBix is showing "The output size is too large (infinite loop or larger text output)".

Why?

Madhu said: (Jul 11, 2015)  
I can't understand the declarations how is it possible to make multiple declarations without being separated by comma.

char far, *near, *ptr1;

I think above declaration is only right.

Bhasker Thapa said: (Aug 27, 2015)  
OUTPUT:

The output size is too large (infinite loop or larger text output).

This is the output when I compiled the above code in India bix compiler, and this does not match with the answer.

Abhisek said: (Mar 28, 2016)  
@Madhu.

First, let's understand how to read these.

Simple on:

char c; // c is a character.

char *ptr; // ptr is a pointer to a char.

char far *fptr; // fptr is a far pointer to char.

Remember when we write double points.

char **dptr; // dptr is a pointer to pointer to a char.

In a similar way.

char far *near *prt1; // prt1 is a near pointer to far pointer to char.

Hope it makes sense.

Vijji said: (Jun 28, 2016)  
Near pointer is used to store 16 bit address.

Far pointer is used to store 32 bit addresses.

Huge pointer is used to store 32 bit addresses.

Sam said: (Aug 30, 2016)  
I don't understand the program statements. Please explain me once.

Jangum said: (Sep 7, 2016)  
Thanks, @Abishek.

Arun Yadav said: (Aug 5, 2017)  
It's valid in turbo c.

For GCC there is no such concept of near and far,
For GCC it's 4 4 4.

Swetha said: (Oct 11, 2017)  
Thanks @Abhisek.

Manoja V. said: (Oct 24, 2017)  
Thanks @Hari.

Pradeep said: (Jun 11, 2018)  
Byte allocation near-2, far, huge-4 so ptr1 refer near so 2bytes, ptr2, 3 refer far & huge so 4 bytes.

Saurabh said: (Dec 10, 2018)  
Can you please tell me how to read the below line?

char far *near *ptr1;

Sureka said: (Jun 28, 2019)  
I am not getting this, please explain me.

Simran said: (Jul 3, 2019)  
Near ptr has a memory of 2bytes. Far and huge ptr have a memory of 4 bytes. Hence the output is 2, 4, 4.

Sabari Ganesh said: (Aug 26, 2019)  
What are near, far and huge pointers?

These are some old concepts used in 16 bit intel architectures in the days of MS DOS, not much useful anymore.

Near pointer is used to store 16 bit addresses means within current segment on a 16 bit machine. The limitation is that we can only access 64kb of data at a time.

A far pointer is typically 32 bit that can access memory outside current segment. To use this, compiler allocates a segment register to store segment address, then another register to store offset within current segment.

Like far pointer, huge pointer is also typically 32 bit and can access outside segment. In case of far pointers, a segment is fixed. In far pointer, the segment part cannot be modified, but in Huge it can be.

Sujata Junare said: (Oct 9, 2020)  
Thanks all.

P.Yoga Priya said: (Mar 6, 2022)  
@All.

Here is my coding.

#include<stdio.h>
int main()
{
int *a;
float *b;
char *c;
double *d;
int i;
float j;
char k;
double l;
a=&i;
b=&j;
c=&k;
d=&l;
printf("%d",sizeof(a));
printf("%d",sizeof(b));
printf("%d",sizeof(c));
printf("%d",sizeof(d));
}


Output:888.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.