0
char a[]="hello";
char *p=a;
printf("%d",sizeof(p));

The expected output is 4 or 8 but the output we get is 2.

Using TurboC3

Bhaktij
  • 109
  • 1
  • 6
  • 2
    I tried it in my MacBook Pro, I get `8` result as expected. did you run it on your laptop or an MCU? I think if the CPU is 16bit, then you have the possibility to get `2`. – Spark.Bao Mar 15 '19 at 10:07
  • Dimly related (via comments): https://stackoverflow.com/questions/55180062/c-printing-the-size-of-character-pointer-in-c TurboC is old and does things in its own way. Try something else. Also, you should probably ask for %lu as the format specifier, not %d. – doctorlove Mar 15 '19 at 10:09
  • Check whether compiler has setting called "memory model" and if it exists - set it to Large to see `4` for result. Or simply change definition to `char far *p=a;` – i486 Mar 15 '19 at 10:21
  • The format for a `size_t` value is `"%zu"`. Turbo C probably doesn't support that, so convert to some time for which a format exists, for example `printf("%lu\n", (unsigned long)sizeof p);` – Keith Thompson Mar 15 '19 at 10:22

1 Answers1

0

I'd highly recommend "Assembly Language: Step by Step by Jeff Duntemann" (not the whole book, just the memory models' section).

Size of pointer depend on how much memory it has to address. You're expecting four bytes, that is 32 bits and it can address 4 Gigs of memory. When you'll do some research, you'll find that there was no 4 Gig memory in any personal computer at the time Turbo C was developed (eighties).

As there were no rams of that capacity (maximum was a few megabytes), and even after that, most of the aplications were too "lightweight" (for backward compatibility) for the newer x86 CPUs and high-end hardware (including memory, of course), applications were given a part of that memory (64k for turbo c as stated by @Nadim). So, why bother making a pointer 4 bytes in size when you already know it's never going to use those last 2 bytes.