Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Beginning C++ question on references and pointers?

I m just starting out in C++ from a background in Java.

Would

"int* a = new int[10]; "

mean that a is a pointer to an array of size 10?

then what would this mean?

for (int i = 0; i < 10; i++){

*(a+i) = 5;

}

Since a is a pointer, would this be changing the memory location address it s storing or the value at that location?

1 Answer

Relevance
  • Anonymous
    3 years ago
    Favorite Answer

    I like the way you phrased your question - it demonstrates that you understand how you read the line to yourself reflects your understanding of what's going on.

    int *a = new int[10]

    I would read this as "a is a pointer to an int, and there is a memory space allocated off the heap that can fit 10 contiguous int (i.e. 10*sizeof(int) bytes), and 'a' points to the first element of that memory space."

    *(a+i) = 5

    This reads "assign the value of 5 to the memory pointed to by the base address 'a' plus i *sizeof(int)."

Still have questions? Get your answers by asking now.