lundi 4 février 2019

How to assign address of array to pointer?

#include <iostream>

int main() {
  int arr[2] = {1, 2};
  int *p;
  // p = &arr; // Does not compile
  p = &arr[0]; // compiles
  std::cout << "&arr    = " << &arr << std::endl;
  std::cout << "&arr[0] = " << &arr[0] << std::endl;
}

When I try to print the address both print the same address. But when I try to assign p = &arr it does not compile. Is there something in standard that says something against assigning address of array to pointer. I just wanted to know the reason why p = &arr does not compile?

Clang actually says error: cannot initialize a variable of type 'int *' with an rvalue of type

Aucun commentaire:

Enregistrer un commentaire