c++ - Pointer Referring to an Array -


recently, i've been trying strengthen skills pointers, , ran following issue:

i have following piece of code, run runtime error. tried looking stuff iterating through pointer represents array, couldn't find anything.

could me find problem?

#include <bits/stdc++.h> using namespace std; int main() {     int * arr;      arr[0]=1;      arr[1]=2;     (int g=0; g<2; g++) cout << arr[g] << '\n'; } 

you haven't declared storage array, nor arr pointing first element of array (dereferencing undefined behavior).


you missing like:

int solve[2]; int * arr = solve; 

or

int solve[2]; int * arr = &solve[0]; 

both ways assign address of correctly allocated storage within scope arr, , dereferencing it, defined behavior.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -