Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

note: previous implicit declaration of ‘point_forward’ was here

Tags:

c

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

like image 935
user2313514 Avatar asked Apr 24 '13 01:04

user2313514


1 Answers

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:

  1. 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.

  2. 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.

like image 85
Timothy Jones Avatar answered Oct 14 '22 05:10

Timothy Jones