Alan Alan - 3 years ago 265
C++ Question

What does "error: taking address of temporary array" mean?

When I try to run the following

// Example program
#include <iostream>
#include <string>

int* x;

int main()
{
x = (int[5]) { 16, 2, 77, 40, 12071 };

std::cout << x;
}


I get the following message
error: taking address of temporary array


What does this mean?

Answer Source

(int[5]) { 16, 2, 77, 40, 12071 } is an anonymous temporary. It goes out of scope once the assignment is complete.

That leaves you with a dangling pointer. It makes no difference that x is in the global namespace.

Use std::vector instead; taking advantage of initialiser-list construction.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download