Pointing to?...refference where?

I just don't get ths behaviour. Lets say we the next snippet:

1
2
3
4
5
6
7
8
9
10
11
  ...........................
  const int MAX = 3;
  int main()
  {
    int var[MAX] = {10, 100, 200};
    int *ptr;
    ptr = var; // works 
    ptr = &var[MAX-1]; // works 
    ptr = var[MAX-1];  // not working
    ..........................
  }


I don't understand why the last assignment doesn't work?

Thank you,
Claudiu.
Im not an expert, and Im honestly not sure that Im right, but Im gonna guess its becuase, MAX is an constant, and you cant just change the constant by value. You have to use "&" so you can get to the adress? Thats my guess, I might have frased things a bit weirdly.

Edit : whoops completely missread the code.
Last edited on
Thank you for your answer but the value of MAX itself is not modified in this operation. :)
var[MAX-1] is the actual value from the array (an int, in this case). If you wanted a pointer to it, you would need to get its address, hence the &.

1
2
3
some_array[some_value]
//is the same as
*(some_array+some_value)
Last edited on
Thank you. I understand very well your reply. In my snippet, the first assignment gets into the ptr the memory location of var[1], that is: ptr = var, however ptr = var[MAX-1] doesn't get the memory location of the last entrance in the row vector. :D, This is my dilema.
Topic archived. No new replies allowed.