MikeD
MikeD

Reputation: 23

How to pass a structure by reference to an included library function?

My C program uses a dynamically allocated array of structures. I'm trying to pass a pointer to a specific member of the array to a function that is in an included library, and I'm getting compiler errors for everything that I try.

BTW, I'm very new to C programing, and learning the hard way by converting an existing Python program to C, in the hope that it will run faster.

The Main.c program includes:

//....
#include "BMS.c"
//....
struct BMS_dev{
    // specific to each battery pack
    char err[16];
//.....
   bool b_lim ;

};
//....
// other constants & variables declared
//....
int main(){ // setup and configure
    char b = 0;
    char dev;
   //......
//more code
    //....
   b = hello_all();
    struct BMS_dev pack[b];  // Array of structures declared here, b could be 1 ,2, 3, 4 
   //......
for (dev=0;dev<b;dev++){  // main loop of program cycles through each device, 0 .. b
        char h = 0;
        id[dev] = 0;
        //.....
        v_cell_d(&pack[dev]);  // function v_cell_d is called, passing reference to pack[b]
        // ...
       //rest of main.//

In code library BMS.c:

bool v_cell_d(struct BMS_dev *pp){
   
    //vc_del = 0;
    pp->vc_max = 0;
    pp->vc_min = 4;
    float lim_low = 2.75 ;//#V
    float lim_high = 3.58 ;//#V
    pp->b_lim = false;
    //......
    return(pp->b_lim)  ;  
  }

The program is compiled with:

gcc -g main.c -l sqlite3 -o Max10 -lm

Compiler errors:

In file included from main.c:14:
BMS.c:13:15: warning: useless storage class specifier in empty declaration
   13 | extern struct BMS_dev;
      |               ^~~~~~~
BMS.c: In function ‘v_cell_d’:
BMS.c:157:7: error: invalid use of undefined type ‘struct BMS_dev’
  157 |     pp->vc_max = 0;
      |       ^~
BMS.c:158:7: error: invalid use of undefined type ‘struct BMS_dev’
  158 |     pp->vc_min = 4;
      |       ^~
BMS.c:161:7: error: invalid use of undefined type ‘struct BMS_dev’
  161 |     pp->b_lim = false;

The first error is caused by trying to tell the compiler that the structure referred to by *pp is declared elsewhere (in main.c); without this, it assumes I'm trying to declare the structure in the function variables list.

How do I make this work?

Upvotes: 2

Views: 71

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597906

Don't #include your .c files, only .h files. The command you use to build the program should specify all of the .c files you want to compile and link together to produce the .exe.

The definition of struct BMS_dev does not belong in main.c, it should be in a separate .h file instead, which can then be #include'd in main.c and BMS.c/.h as needed.

Also, you should have a .h file for BMS.c to declare v_cell_d() so main.c knows about it.

Try something more like this instead:

BMS_dev.h

#ifndef BMS_dev_h
#define BMS_dev_h

struct BMS_dev{
    // specific to each battery pack
    char err[16];
    //...
    bool b_lim;
};

#endif

main.c

#include "BMS.h"
//...

int main(){
    //...
    // use BMS_dev and v_cell_d() as needed...
    //...
}

BMS.h:

#ifndef BMS_h
#define BMS_h

#include "BMS_dev.h"

bool v_cell_d(struct BMS_dev *pp);

#endif

BMS.c:

#include "BMS.h"

bool v_cell_d(struct BMS_dev *pp){
    //...
}

gcc command:

gcc -g -l sqlite3 -o Max10 -lm main.c BMS.c

Upvotes: 2

Related Questions