when i try to assign values to dynamic array using function scanf, i’ll get this error “Signal received: SIGSEGV (Segmentation fault)” after first assign, but when I use cin, it works and all values are assigned correctly. Why? Is the problem in my code?
int main(int argc, char** argv) {
int r = 5;
short* pole[r];
for(int x = 0;x<r;x++){
pole[x] = new short[r];
for(int y = 0;y<r;y++){
scanf("%d",pole[x][y]); //error
//cin >> pole[x][y]; //OK
}
}
}
You’re missing an ampersand, and the
%dshould be%hd(forshort int):Your original version was passing the value of
pole[x][y], whereasscanf()expects a pointer. Also, the type specifier (%d) was wrong for the type of&pole[x][y].