C Programming - Complicated Declarations - Discussion
Discussion Forum : Complicated Declarations - Find Output of Program (Q.No. 1)
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;
}
Discussion:
31 comments Page 1 of 4.
Sabari Ganesh said:
6 years ago
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.
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.
(2)
Abhisek said:
9 years ago
@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.
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.
P.yoga priya said:
3 years ago
@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.
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.
Vishal and sandeep said:
1 decade ago
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.
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.
Sakshi chopra said:
1 decade ago
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.
Far pointer-refer to data segment $ points to gigabyte, occupy 4 byte.
Huge pointer-similar to far pointers, but work slowpy, occupy 4 byte.
Bhasker thapa said:
10 years ago
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.
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.
Madhu said:
1 decade ago
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.
char far, *near, *ptr1;
I think above declaration is only right.
Satish Bhati said:
1 decade ago
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?
And GCC Compiler (32 Bit Linux Platform) of IndiaBix is showing "The output size is too large (infinite loop or larger text output)".
Why?
Vijji said:
9 years ago
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.
Far pointer is used to store 32 bit addresses.
Huge pointer is used to store 32 bit addresses.
Pradeep said:
7 years ago
Byte allocation near-2, far, huge-4 so ptr1 refer near so 2bytes, ptr2, 3 refer far & huge so 4 bytes.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers