-1

I'm working through some code for a class I'm taking, and since I'm not familiar with C++ I am confused by subscripting pointers.

My assumptions:

& prefixed to a variable name, gives you a pointer to the memory address of that value and is roughly inverse to * prefixed to a variable name, which in turn gives you the value that resides at a particular memory address.

Pointers are subscriptable, so that ptr[0] == ptr and ptr[n] == ptr + (n * data_bytes), where data_bytes depends on the particular type of the pointer (eg. 4 bytes wide for 32-bit ints).

The Problem:

When I want to subscript the memory address, I have to put it in a variable first, and I don't understand why I can't just subscript the variable directly.

Minimal Code Example:

#include <iostream> using namespace std; int main() { int val = 1; int *val_ptr = &val; cout << &val << endl; // works cout << val_ptr[0] << endl; // does not work // cout << &val[0] << endl; return 0; } 

Addendum

I am aware of some related questions, but they didn't really help me understand this specific issue.

I looked at:

and while this is almost certainly a duplicate (I will hardly be the first person that's confused by pointers), or maybe even has an answer that I overlooked in the linked questions, I'd appreciate any help!

8
  • 5
    Operator precedence. [] is considered before &. Do (&val)[0] instead. Commented May 5, 2022 at 15:31
  • When I want to subscript the memory address, I have to put it in a variable first, and I don't understand why I can't just subscript the variable directly. Commented May 5, 2022 at 15:35
  • It's in there, but I agree, that it doesn't stand out enough. Would you think putting this at the start would improve readability/understandability? Commented May 5, 2022 at 15:36
  • 1
    ptr[0] == ptr and ptr[n] == ptr + (n * data_bytes) are both incorrect. We don't say that an address and the value at that address are == equal. For a pointer p and integer i, by definition p[i] == *(p + i) == *(i + p) == i[p]. Note that int(p + i) == int(p) + i * sizeof(*p), that is, the multiplication by element size applies during pointer arithmetic. So ptr + (n * bytes_per_element) is wrong, it would multiply by element size twice. Commented May 5, 2022 at 16:15
  • 1
    ptr[n] == *(ptr + n), that's actually the definition of the [] operator (array subscripting). The parentheses are needed, due to operator precedence (which is what your question turned out to be about), if you write just *ptr + n that would be the same as ptr[0] + n not ptr[0 + n] Commented May 5, 2022 at 17:14

1 Answer 1

1

As per Ted Klein Bergmann's comment, there was a problem with operator precedence.

[] is considered before &. Do (&val)[0] instead.

So a working example would be

#include <iostream> using namespace std; int main() { int val = 1; // does work now cout << (&val)[0] << endl; return 0; } 
Sign up to request clarification or add additional context in comments.

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.