Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
941 views
in Technique[技术] by (71.8m points)

c - note: previous implicit declaration of ‘point_forward’ was here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...