Home > Software design >  passing structs to file functions
passing structs to file functions

Time:11-03

hi guys im currently learning c and i have to pass a struct between multiple files for my project in uni . no matter what i do i keep getting errors when i try to pass a struct to a function. i have tired moving the function between being a global variable in main.c and i have tried putting it in a .h file, but i cannot figure it out, i keep hitting the same error messages when i try to gcc it . im having no problems using it in my main file but passing my variable to my functions in external files is where its getting errors i i will include my files that are relevant and my error messages when i gcc it. any help will be much appreciated cheers .

main.c

int main( int argc, char *argv[] ) {
    const int bufferSize=10;
    int const nBuffer = 50;
    char iptName [nBuffer];

    empInfo* info;
    info = (empInfo*)malloc(bufferSize * sizeof (empInfo));
    if (info == NULL){
        printf("*malloc* array not allocated\n");
    }
    printf("malloc worked");
    nameFinderPrinter(iptName,info,bufferSize);
    return;
} 

fileFunctions.c

void nameFinderPrinter(char name[],empInfo* info, int size) {
   bool nameTF;

   for (int i = 0; i < size; i  ){
       if (strcmp(name, info[i].names )== 0);

       printf("%s age is %d wage is %f\n", info[i].names, info[i].ages, info[i].wages);
       nameTF=true;
   }
   if(nameTF ==false){
   printf("%s could not be found\n",name);}
   return;
}

struct.h

typedef  struct employeeInfo   
{
    char names[50]; 
    int ages;
    float wages; 
} empInfo; 

error message on gcc

 In file included from main.c:7:
struct.h:1:17: error: redefinition of 'struct employeeInfo'
    1 | typedef  struct employeeInfo
      |                 ^~~~~~~~~~~~
In file included from fileFunctions.h:7,
                 from main.c:6:
struct.h:1:17: note: originally defined here
    1 | typedef  struct employeeInfo
      |                 ^~~~~~~~~~~~
struct.h:6:3: error: conflicting types for 'empInfo'; have 'struct employeeInfo'
    6 | } empInfo;
      |   ^~~~~~~
struct.h:6:3: note: previous declaration of 'empInfo' with type 'empInfo' {aka 'struct employeeInfo'}
    6 | } empInfo;
      |   ^~~~~~~
main.c: In function 'main':
main.c:86:29: warning: passing argument 2 of 'nameFinderPrinter' from incompatible pointer type [-Wincompatible-pointer-types]
   86 |   nameFinderPrinter(iptName,info,bufferSize);
      |                             ^~~~
      |                             |
      |                             empInfo * {aka struct employeeInfo *}
fileFunctions.h:11:45: note: expected 'empInfo *' {aka 'struct employeeInfo *'} but argument is of type 'empInfo *' {aka 'struct employeeInfo *'}
   11 | void nameFinderPrinter(char name[],empInfo* info, int size);

i have tried using pointers to 'info[10]' and rewriting how i pass it to the function in every way i can think of only way that works is to send the internal information of the struct bit by bit like 'info[i].name' i have attempted to make info a global variable and accessing it in the function globally. i am sorry if its messy i am still getting used to how to properly present my code on here and in general

CodePudding user response:

You need to use "guards" around you header files:

#ifndef INCLUDE_STRUCT_H
#define INCLUDE_STRUCT_H
typedef struct employeeInfo   
{
    char names[50]; 
    int ages;
    float wages; 
} empInfo; 
#endif

This prevents the content from being inserted twice. You included the header in main twice, once directly and once indirecty by including "fileFunctions.h", which itself includes it directly. This leads to the struct being defined twice in the same file. To prevent this these "guards" are used so it can only be included once.

CodePudding user response:

Some compilers support "#pragma once":

#pragma once

typedef struct employeeInfo   
{
    char names[50]; 
    int ages;
    float wages; 
} empInfo; 

which is simpler than defining header guards as suggested by Tenobaal.

  • Related