0
#include<stdio.h>
#include<conio.h>

void main()
{
  int a[5]={1,2,3,4,5};
  int (*p)[5];
  int *p1[5];
  clrscr();

  printf("%d\n",(int)sizeof(a)); // 10
  printf("%d\n",(int)sizeof(p));  //2
  printf("%d\n",(int)sizeof(p1)); //10
  getch();
}

First output is 10 because each integer is of 2 bytes and hence 5 integers would take 10 bytes.

I am not able to understand the 3rd output which is 10. Here we have array of pointers each pointing to an integer. Size of pointer(or address) in my system is 32 bits (4 Bytes) . So the output should be 5*4=20 right as we have 5 pointers each of 4 bytes ?

23
  • 2
    Is this C or C++? Are you using Turbo C++ by any chance? Commented Jul 8, 2017 at 20:33
  • 2
    Your system may be 32 bits, but TurboC++ came from the time before time when 16 bit compilers were all the rage. Commented Jul 8, 2017 at 20:39
  • 2
    @WeatherVane: OP states he uses TurboC++. Until further information it is more likely C++ (resp. that ancient dialect of TC++) than C. Commented Jul 8, 2017 at 20:40
  • 4
    Stop using it and get yourself a compiler not made in the previous millennium. Commented Jul 8, 2017 at 20:51
  • 2
    @Xylene23 That's a new question. Make a new question to ask new questions. Better still, don't ask it. This should be covered in the first few chapters of any text book that isn't outright fraudulent. Commented Jul 8, 2017 at 20:58

3 Answers 3

7

The second printout shows the size of a pointer on your machine is 2 bytes.
Array int *p1[5]; has 5 elements, not 10.

5 * 2 = 10.

Sign up to request clarification or add additional context in comments.

6 Comments

See my edit now. Size of pointer depends on the system right? In my case it is 32 bit so 4 bytes. So for an array containing 5 pointers, shouldn't the size of array be 5*4=20?
The second printf would print 4 if that was true.
In the pointer array, what will be the increment size? Will it be according to the type which it is pointing to (int in this case) or according to the size of pointer which is 16 bits or 2 bytes ?
It will be sizeof(int *).
All it says is a pointer takes two bytes. That does not say anything about the width of a byte, though.
|
2

Size of pointer(or address) in my system is 32 bits

Turbo C++ is an MS-DOS program. It's unable to run on a modern OS directly. Your OS creates, in a way completely transparent to you, an emulated 16-bit MS-DOS machine to run your outdated stuff on. So no, the size of pointers on your (emulated) system is 16 bits. You are using a 16-bit system.

Turbo C++ is actually capable of using 32-bit pointers if you switch code generation to "large" or "huge" mode. The system is still a 16-bit one, it just has a weird addressing scheme with "long" pointers.

On a side note, using Turbo C++ in this current millennium is not recommended (warning: shameless plug).

Comments

1

You are on a 16 bit system

All three numbers are consistent with each other. But you seem to be using a 16 bit system. So your pointers use 2 bytes. And 5 elements times 2 bytes each equals 10 bytes in total.

3 Comments

I am not using a 16 bit system but still turbo C++ shows that it's 16 bit. I don't know the reason behind that.
@Xylene23 it might be that your system can process 32 bit numbers in hardware. But your pointers use 2 byte (which is 16 bit). So I think it is called a 16 bit system (actually by definition).
@Xylene23 Turbo C++ was built to compile programs that would run on a 16 bit operating system. It doesn't know that anything other than 16 bit addressing exists. Your much more advanced computer and operating system are kind enough to hide the existence of the other 16 bits from the programs you compile with Turbo C++ and allow them to run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.