pointers - C++ segmentation fault while passing address values between functions -



pointers - C++ segmentation fault while passing address values between functions -

i trying write simple c++ algorithm solve sudoku. trying pass address values between different functions segmentation fault @ runtime. (needless say, not quite experienced :))

the code manage pass address of a[0] main function , can read values using pointers within main. when seek pass address solve function, gives segmentation fault.

(also secondary question, can read values correctly in main, using cout << *(a+5) etc. correctly (commented out in main), when seek print 81 values stored using loop, gives out nonsense values (again, commented out in code). code works literals *(a+3) or a[3], not when int gets involved for(int i, whatever) cout << *(a+i);)

#include <iostream> using namespace std; int * get_input(); void solve(int *); int main() { int * a; = get_input(); //cout << *a << " " << *(a+1) << " " << *(a+2) << " " << *(a+3) << " " << *(a+4); //for (int = 0 ; < 81 ; i++) {if (i%9 == 0) cout << "\n"; cout << a[i] << " ";} solve(a); return(0); } int * get_input () { int a[81]; getinput: (int = 0 ; < 81 ; i++) {a[i] = 0;} (int = 0 ; < 81 ; i++) {cin >> a[i];} print: (int = 0 ; < 81 ; i++) { if (i%27 == 0){cout << "\n";} if (i%9 == 0) {cout << "\n";} if (i%3 == 0) {cout << " " << a[i];} if (i%3 != 0) {cout << a[i];} } cout << "\n\ncheck:\n1- fix\n2- reset\n3- confirm\n\n"; int check = 0; cin >> check; if (check == 1) { int input[3] = {-1, -1, -1}; while (true) { cin >> input[0] >> input[1] >> input [2]; if (input[1] == 0) goto print; a[(input[2]-1)+((input[1]-1)*9)] = input[0]; } } if (check == 2) goto getinput; if (check == 3) homecoming a; } void solve(int * a) { bool matrix[9][9][9]; (int = 0 ; < 81 ; i++) {for (int j = 0 ; j < 9 ; j++) {matrix[(i-i%9)/9][i%9][j] = true;}} (int = 0 ; < 81 ; i++) { if (a[i] == 0) continue; else { (int j = 0 ; j < 9 ; i++) { matrix[(i-i%9)/9][j][a[i]] = false; matrix[j][i%9][a[i]] = false; matrix[((i-i%9)/9)-((i-i%9)/9)%3+j%3][i%9-(i%9)%3+(j-j%3)/3][a[i]] = false; } } } (int = 0 ; < 9 ; i++) { (int j = 0 ; j < 9 ; j++) { cout << matrix[i][j][1] << " "; } cout << "\n"; } }

make a array in get_input() function static:

int a[81];

should

static int a[81];

this works because static keyword ensures allocated memory block (the array a ) remain allocated after function returns. "because i'm not finished yet" (for example, can count how many times function called way), can also, legitimately, used ensure homecoming value of function survives end of function.

marginally improve declare array @ main level, , pass pointer get_input() , solve() functions. way create explicit in code array "live duration of program" - , that's practice.

c++ pointers fault

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -