简体   繁体   中英

Why is the function initialized in the if statement?

For some reason, I'm getting an error that says conflicting types for 'get_property' because the function is reinitialized below. Why is this happening and how can I fix this?

/* find the longest client_machine name */
for (i = 0; i < client_list_size / sizeof(Window); i++)
{
    gchar *client_machine;
    if ((client_machine = get_property(disp, client_list[i],
                                       XA_STRING, "WM_CLIENT_MACHINE", NULL)))
    {
        max_client_machine_len = strlen(client_machine);
    }
    g_free(client_machine);
}

Details of the error:

/home/raphael/Desktop/Projects/sus/util.h:134:8: error: conflicting types for ‘get_property’
  134 | gchar *get_property(Display *disp, Window win,
      |        ^~~~~~~~~~~~
/home/raphael/Desktop/Projects/sus/util.h:64:31: note: previous implicit declaration of ‘get_property’ was here
   64 |         if ((client_machine = get_property(disp, client_list[i],
      |                               ^~~~~~~~~~~~

What you need is a function prototype in the beginning of the file (before line 64, where you are calling it inside that if ).

In computer programming, a function prototype or function interface is a declaration of a function that specifies the function's name and type signature, but omits the function body. While a function definition specifies how the function does what it does (the "implementation"), a function prototype merely specifies its interface, ie what data types go in and come out of it.

In short, the compiler does not know what your function returns or expects as parameters when you call it in line 64. So, in order to inform the compiler on how to interpret your function, put this somewhere in the beginning of the file, ideally right after include or struct definitions, along with global variables:

 gchar *get_property(Display *disp, Window win, ... )

Note that for this to work, the gchar type must have been defined already (you can not use a type if you have not defined the type yet...). In general, I try to have a file structure similar to this:

1. Includes
2. Defines
3. Structs, enuns and unions
4. Global variables
5. Externs
6. Prototypes
7. Functions definitions

By the way, nothing wrong with the if .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM