I can't seem to get this recursive function to compile properly, and I'm not sure why. The code is as follows:
void point_forward (mem_ptr m) {
mem_ptr temp;
temp = m->next;
if (temp->next != NULL) point_forward(temp);
m->next = temp->next;
}
My compiler returns this:
mm.c:134:6: warning: conflicting types for ‘point_forward’ [enabled by default]
mm.c:96:2: note: previous implicit declaration of ‘point_forward’ was here
The key is in this:
previous implicit declaration of ‘point_forward’ was here
On line 96 you have:
point_forward(m); // where m is a mem_ptr;
Since the compiler hasn't yet seen a function declaration for point_forward(m)
, it "implicitly defines" (ie, assumes) a function that returns an int:
int point_forward(mem_ptr m);
This conflicts with the definition later:
void point_forward (mem_ptr m) {
To fix this, you can either:
Put an explicit declaration somewhere before line 96: void point_forward(mem_ptr m);
This will tell the compiler how to treat point_forward()
when it sees it on line 96, even though it hasn't yet seen the function implementation.
Or, define the whole function above line 96 (move the function definition from line 134 onwards to above line 96).
Here is a little bit more about declaring functions.
Generally, for style, I would either:
If you don't want to use point_forward()
in any other C files, define it in full:
static void point_forward(mem_ptr m) { ..function body goes here.. }
at the top of the source file.
If you want to use point_forward()
in other C files, put a forward declaration:
void point_forward(mem_ptr m);
in a header file for other files to include.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With