1

I started to read a few articles about pointers in C and I've got one example that I don't understand. What should be the output of following code..??

    main()
     {
      char far *s1 ,*s2;
      printf("%d,%d",sizeof(s1),sizeof(s2));
     }

OUTPUT-4,2

According to me, value returned by both sizeof() functions should be 4 because a far pointer has 4 byte address.

but the answer in solution manual is 4,2. Can any one explain ?? can anyone plz explain>???

Massimiliano
  • 16,770
  • 10
  • 69
  • 112
DD24
  • 61
  • 7
  • 5
    Must be an old book/article(?) –  Jul 14 '13 at 20:01
  • 2
    This is not valid C++ and `sizeof` returns an unsigned value. – chris Jul 14 '13 at 20:01
  • 3
    `far` is a non-standard anachronism from the 1980s and 8086 CPUs. – Paul R Jul 14 '13 at 20:03
  • 1
    That's that same as `char far* s1; char * s2;` – Jim Balter Jul 14 '13 at 20:03
  • 1
    If you have a compiler that will take `far`, you should probably think about moving up to something released more recently than 20 years ago. – Carl Norum Jul 14 '13 at 20:25
  • You didn't pay attention to my line *move on in life* ... Now, seriously , these all things have become non-standard .... have a read at the C99 standard at least http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf ...... or you can buy the C11 standard its the latest . – 0decimal0 Jul 15 '13 at 03:36
  • Throw away that book/article. "far" and "near" are not in standard C (never have been) and any discussion of them only confuses you when you're trying to understand pointers. – Rhialto supports Monica Jul 15 '13 at 10:36
  • Far pointers are no longer in practice I think. –  Jul 15 '13 at 12:55

1 Answers1

6

It's the same as writing

char far *s1;
char *s2;
the "far" is not distributed across all variables, e.g.
char far *s1, ch;

far makes no sense on a normal character ch.

Hence s2 is not a "far" pointer, and is handled as a "near" pointer, which is 16 bits wide in your target.

franji1
  • 3,088
  • 2
  • 23
  • 43
  • can you plz explain the concepts of bits associated wid a far or near pointer,..?? – DD24 Jul 14 '13 at 20:15
  • 1
    I am familiar with old Intel processors that had memory models such that data could be "near" (within the same 64K Byte segment) vs. "far", not within the current 64K byte segment. Since memory was expensive, most apps did not need much data anyway. To save even MORE memory, it let you specify default pointers to use just 16 bits, and then using a pseudo keyword "far" wherever the API needed a 32 bit pointer. You may want to ask this as another question to get more details (especially with other targets/compilers). – franji1 Jul 14 '13 at 20:26
  • 1
    Don't bother asking, this question has already been asked/answered http://stackoverflow.com/questions/1749904/difference-between-far-pointer-and-near-pointer-in-c – franji1 Jul 14 '13 at 20:44
  • @DD24 why bother asking about far pointers when they are not in practice. –  Jul 15 '13 at 12:56
  • 1
    @AmoghDikshit Sadly, there's a lot of legacy code out there that was ported from 16 bit DOS/Windows (e.g. MFC) that still contain the pseudo keyword "far" (or in MFC's case "FAR"), and it is compiled out, i.e. replaced with nothing by the preprocessor. Even though NEW code should never utilize it, there's a somewhat decent chance that anybody doing maintenance on "old" code will see this. Hence, these SO users may want to better understand WHY they see this obsolete construct. – franji1 Jul 15 '13 at 13:23