user19799056
user19799056

Reputation:

Why does the pointer get incremented by a random value?

int arr[]={2,3,5,7};
int (*p)[4];
p=&arr;
cout<<p<<endl;
p++;
cout<<p<<endl;

Look at this simple piece of code where I'm trying to use a pointer to point to a whole integer array. The array contains 4 elements so it's size in memory should be 4*4=16 bytes. When I run it, the output is:

0x7ffe789ed080
0x7ffe789ed090

Expected output:

0x7ffe789ed080
0x7ffe789ed096

Upvotes: 0

Views: 46

Answers (1)

Silvio Mayolo
Silvio Mayolo

Reputation: 70397

Memory addresses are generally output in hexadecimal. The number 16 is written in hexadecimal as 0x10, which is the difference between those two numbers. It's just a different format than you were expecting.

Upvotes: 1

Related Questions