I’m trying to write a simple delay function. It takes two arguments: a pointer to a void function and an unsigned integer (unsigned int) specifying how many times the function should be executed. However, I’m encountering a dead loop, preventing the code after the function call from executing.
void delay(void (*func)(void), unsigned int time) {
unsigned int i, j;
for (i = 0; i < time; i++) {
func();
for (j = 0; j < 10000; j++);
}
}
In an attempt to avoid using functions, I implemented the following code:
void main() {
unsigned int i, j;
for (i = 0; i < time; i++) {
for (j = 0; j < 10000; j++);
func();
}
// other codes to be executed
}
I was surprised to find that this code avoids the dead loop I encountered previously. I’m confused about this. Maybe it has something to do with the function pointer? (The complier is Keil C51)
Rob Zahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.