In below code, I expect 1 to be initialized in all the 10 elements in x array. But, it doesn't seem to be working. May I know what I am missing here

int main() {
int *x = new int[10];
//std::cout<<"address of x: "<<&x<<std::endl;

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

for(int i = 0; i < 10; ++i)
    std::cout<<i<<" is "<<x[i]<<std::endl;

return 0;
}

In socratic fashion, I'd ask where does x point at the start of the second loop?

int *x = new int[10]{1,1,1,1,1,1,1,1,1,1}; // missing this: g++ -std=c++11

for(auto &it : x) std::cout << it << " ";

delete x;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.