Zephyr
Zephyr

Reputation: 1621

size of array of integer pointers

#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 ?

Upvotes: 0

Views: 225

Answers (3)

n. m. could be an AI
n. m. could be an AI

Reputation: 119847

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).

Upvotes: 2

plats1
plats1

Reputation: 930

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.

Upvotes: 1

LogicStuff
LogicStuff

Reputation: 19607

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.

Upvotes: 7

Related Questions