0

Here is the code of my tutorial

#include<iostream>
using namespace std;

int main()
{
    int arr[5] = {8,2,9,4,5},a,b,*ptr;
    int c = 5;

    ptr = arr;
    a = *ptr + 10;
    ptr = &c;   
    ptr += 2;
    cout<<*(ptr+1);
}

I'm a little confused why the output is 9. When ptr is pointing to c, which is valued 5, how can the pointer be incremented +2 since c is not an array?

3
  • 3
    Yikes! There is only one c. When you set ptr = &c;, the ptr points to (holds the address of) c. Doing ptr += 2; puts you 2 integers (8-bytes) after c. (that is undefined behavior -- that's not part of c -- no pun intended, well maybe...) Commented Mar 10, 2020 at 5:37
  • 1
    Where did you get this code from? Commented Mar 10, 2020 at 5:42
  • Here is a pointer basics post that may help Can I dereference the address of an integer pointer? Commented Mar 10, 2020 at 5:43

4 Answers 4

2

Undefined behavior

Let's break it down:

int arr[5] = {8,2,9,4,5},a,b,*ptr;
int c = 5;

ptr = arr;      // ptr = address of arr[0]
a = *ptr + 10;  // a = (arr[0] + 10) = (8 + 10) = 18
ptr = &c;       // ptr = address of c
ptr += 2;       // UNDEFINED BEHAVIOR, c is not an array
cout<<*(ptr+1); // COULD PRINT ANYTHING

Not that we've established undefined behavior, the language lawyers can't sue me to explain what might be happening:

The variables of main are possibly piled onto the stack as follows. It's convenient that the elements on the stack are integer for this simple example:

0x100 c 
0x104 arr[0]
0x108 arr[1]
0x10C arr[2]
0x110 arr[3]
0x114 arr[4]
0x118 a
0x11C b
0x120 ptr // address of the variable ptr, not what ptr points to

Hence, ptr+2 is the same address as the arr[1] in the array, which holds a value of "2". And then then the additional (ptr+1) expression is the address of arr[2], which holds the value 9.

But that's because you are getting lucky. Any other system or compiler could crash the program or make the lights dim in your house. Such is the nature of undefined behavior.

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

8 Comments

ptr += 2 is not undefined behavior. It's only undefined once you dereference.
Language lawyers. I can never escape them.
It's the only taste of control I have in this chaotic world :(
@John I believe the behavior of ptr += 2 is undefined, though ptr += 1 would be OK. See 8.7 [expr.add] paragraph 4. The "(possibly-hypothetical) element" wording refers only to a single element past the end of the array. (c is treated as a 1-element array.)
@Keith that's such a hairy sentence. I think you're right. Footnote 86) in that was key to me being able to parse it.
|
0

Well, the pointer is not pointing to c.

when you did ptr += 2; the pointer got incremented from c and is now pointing to a memory space having a garbage value.

When I ran the code I got 612602545 as output which clearly is a garbage value. The output you get will differ each time you run the code or from system to system.

Comments

0

Output of your program is not deterministic. ptr pointing to c has been incremented by 2. As ptr was not pointing to contiguous memory structure (Like array), it is not possible to determine what ptr is pointing at after increment. As it is int pointer, every increment will cause it to jump by sizeof(int). Derefrencing ptr after increment will try to read next sizeof(int) bytes as int. So you can get any garbage value.

Comments

0

I think you are missing the * in ptr

#include<iostream>
using namespace std;


int main()
{
    int arr[5] = {8,2,9,4,5},a,b,*ptr;
    int c = 5;

    ptr = arr;
    a = *ptr + 10;
    ptr = &c;
    (*ptr) += 2;
    cout<<*(ptr+1);
}

It will print 8, But your program undefined behavior due to this line

  ptr += 2; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.