简体   繁体   中英

typedef in C and function pointers

I have some structures and corresponding functions that operate on them written in their own header and c file.

I wondered if it was possible to create a new header and c file for code that would "inherit" those particular types and functions with a new appropriatly descriptive declaration? Is it possible to typedef functions and structs in this manner in order to reuse the code?

I have looked into function pointers but I am not sure if this is the correct tool to achieve what I am after. I guess one other option is to refactor the code so that the names are generic.

Code example:

// function1.h

typedef struct src_data {
      ...
} src_data;

src_data* process_src_data(...) {
   ...
   return new_data;
}
// function2.h

#include "function1.h"
typedef src_data dest_data;

typedef dest_data* (*process_dest)(void);
process_dest process_dest_data = &process_src_data;

usage would then be as follows:

#include "function1.h"
#include "function2.h"

src_data *sourceData = process_src_data(...);

dest_data *destinationData = process_dest_data(...); 

A crude and simple solution:

// function2.h
#define process_dest_data process_src_data

A slightly nicer approach is using static inline functions as wrappers:

// function2.h
static inline dest_data* process_dest_data(void) {
    return process_src_data();
}

Using inline will help you avoid warnings about non-used functions.

Both will likely result is similar object code.

There is a subtle advantage of a macro over a wrapper. Assume that process_src_data has external linkage. Expression &process_dest_data may have a different value in each translation unit. If macro were use then the value would be the same, equal to &process_src_data .

if it was possible to create a new header and c file for code that would "inherit" those particular types and functions with a new appropriatly descriptive declaration?

C is a very (nowadays;) simple language that you have to write a lot yourself. Other language - notably thinking of object-oriented languages, C++, Java - have a builtin "inheritance" feature that the language, by itself, allows to "import" all function from one place to another.

C does not have this feature. In C, you have to write it all, from the top yourself.

I am not sure if this is the correct

  1. Do not define functions in a header file. Do not:

// function1.h
src_data* process_src_data(void) { /* NO */ }

instead put a funciton declaration in a header and definition in a source file.

// function1.h
src_data* process_src_data(void); //ok
// function1.c
src_data* process_src_data(void) { ok(); }

otherwise it's not "correct", in a sense multiple .c files that include that header linked together will cause "multiple deifnitions" problems.

  1. Similar with function pointer declaration:

// function2.h
process_dest process_dest_data = &process_src_data;

Either make the function pointer static , or move it to separate C source file and add extern to the header. Additionally add const to it if it's intended to be constant, so it can be optimized to read-only section and not use RAM.

// function2.h
extern const process_dest process_dest_data;
// function2.c 
const process_dest process_dest_data = &process_src_data;

tool to achieve what I am after.

Function pointers take memory. Calling a function pointers needs to dereference them. They are hard to optimize.

It's typical in C to write short static wrapper functions:

// function2.h
static process_dest process_dest_data(void) {
     return process_src_data();
}

If the function is short, it will be inlined by the compiler and removed from the resulting executable. For longer, just write a regular function that calls the underlying implementation.

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