Hi
So I am now working on writing my sudoku program in C so that I can run it on my Nintendo DS. This will be a major step for me, I feel, as it will represent the first time my very own code will be able to interact in a meaningful way with the real world. Well... not THAT meaningful. I mean, it doesn't solve cancer, but it's a step in the right direction at least!
Now, however, is a time for tears and not a time for speculation about celebration. C is not a language I have studied much: I read a few tutorials to get a sense of how things go, and then I just started coding like a fox. Here is a function that is getting on my nerves. It looks to me like it should work, and it works fine the first time I call it but never the second time.
This function is passed a sudoku cell, which is essentially a three dimensional array of integers, and counts the number of cells at the lowest level. It does a recursive thing: if a cell has kiddies, it will try to call itself on the kiddies, and if those kiddies have kiddies it will call itself on them.
int getNumCells(Cell* subject) {
int r = 0; // the return value
int i;
Cell cell;
if(subject->isParent == true) {
for(i = 0; i < subject->cells_length; i++) {
cell = subject->cells[i];
r += getNumCells(&cell);
}
} else {
r = CELL_CAPACITY;
}
return r;
}
One problem I have is knowing whether something aught to be a pointer or not. Like, each Cell has an array of cells, and I assign a member i of that array to a Cell, and then pass a pointer to that Cell to the next iteration of getNumCells. But I wonder when I look at that: what did I end up passing? Is it really a pointer to cell... Does ->cells[i] called on Cell* subject return a Cell or a Cell*...
I guess it SHOULD return a Cell*, being as you don't want to be passing around whole structs... so is the assignment Cell cell = subject->cells[i] doing what I think it is doing?
This is the state of my mind on my third day of C programming. Perhaps things will get better soon. I mean, that function returns the expected number (81) once, and then starts returning unexpected numbers. I suppose that's a success of sorts.
z.