Reputation: 874
I've got the assignment to create a very fast nqueens program. So far, Bruteforce algorithm and Backtrack are working, but I've found a C program which seems to be faster. Now I've tried to convert it from C to Java, however I'm unable to convert the pointers, as I'm not understanding what's happening with pointers. These are the lines that have to be converted:
Edit: I've updated the conversions to what I've understood:
final int[] aStack = new int[MAX_BOARDSIZE * 2];
int bitfield; //That's what I've got so far
//Conversion 1:
register int* pnStack; -> int pnStack;
//Conversion 2:
pnStack = aStack + 1; -> pnStack = 1;
//Conversion 3:
pnStack = aStack + 1; -> pnStack = 1;
*pnStack++ = 0; -> aStack[pnStack++] = 0;
//Conversion 4:
bitfield = *--pnStack; -> bitfield = aStack[--pnStack];
//Conversion 5:
*pnStack++ = bitfield; -> aStack[pnStack++] = bitfield;
//Conversion 7:
pnStack == aStack -> pnStack == aStack[0] ???????
A solution would be nice, but I'll try to find it myself if you can explain me what's going on here. Pointers are the reason I never actually learned c and c++.
Regards, Dennis Fischer
Upvotes: 1
Views: 865
Reputation: 24780
Inside an array, interpret a pointer just as you would use an index inside the array.
The *
means the value in the current position of the index.
So
pnStack = aStack; <-- int pnStack = 0;
pnStack++; <-- pnStack++;
bitfield = *pnStack <-- bitfield = aStack[pnStack];
bitfield = *--pnStack <-- bitfield = aStack[--pnStack]; (first decrement index, then retrieve the value)
Upvotes: 3