Thread: Struct array to function on a header file.

  1. #1
    Registered User
    Join Date
    Nov 2013
    Location
    Curitiba, Brazil, Brazil
    Posts
    16

    Struct array to function on a header file.

    Hello,

    I just can't make my code work.

    I need to pass a struct to a .h file.

    Here is my main:

    Code:
    #include "calculos.h"
    #include "auxiliar.h"
    
    struct dados{
        int idade;
        char sexo;
        int periodo;
        float coeficiente;
        float nota;
        float frequencia;
        int preparacao[6][5];
        int regencia[6][5];
        int regencia2[5][5];
        int apoio[7][5];
        int extraclasse[4][5];
        int integracao[6][5];
        int sugestao[300];
    };
    
    
    
    int main(){
        struct dados *usuario;
    
    
        //just a bunch of working code that alloc,fill and print the array of struct dados
    
    
        calculos(k,usuario);
        system("pause");
        return 0;
    }
    
    
    END_OF_MAIN();
    and I have a calculos.c file, with.

    Code:
    #include<stdlib.h>
    
    
    void calculos(int size, struct dados *usuario){
    
    
        int i,a;
        for(i=0;i<size;i++){
            a=usuario[i].idade;//i get an error right here
        }
    }
    and calculos.h
    Code:
    void calculos(int tamanho, struct dados *usuario){}
    ERROR
    Code:
    ||=== Build: Debug in hiltão2 (compiler: GNU GCC Compiler) ===|
    C:\Users\Juscelino\Desktop\hiltao2\calculos.c|3|warning: 'struct dados' declared inside parameter list [enabled by default]|
    C:\Users\Juscelino\Desktop\hiltao2\calculos.c|3|warning: its scope is only this definition or declaration, which is probably not what you want [enabled by default]|
    C:\Users\Juscelino\Desktop\hiltao2\calculos.c||In function 'calculos':|
    C:\Users\Juscelino\Desktop\hiltao2\calculos.c|7|error: invalid use of undefined type 'struct dados'|
    C:\Users\Juscelino\Desktop\hiltao2\calculos.c|7|error: dereferencing pointer to incomplete type|
    C:\Users\Juscelino\Desktop\hiltao2\calculos.c|5|warning: variable 'a' set but not used [-Wunused-but-set-variable]|
    ||=== Build failed: 2 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
    Last edited by Gainborought; 03-13-2014 at 07:17 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well first an .h file shouldn't have any executable code. Perhaps the structure definition should be in the .h file and the function in another .c file.


    Jim

  3. #3
    Registered User
    Join Date
    Nov 2013
    Location
    Curitiba, Brazil, Brazil
    Posts
    16
    Thank you Jim, anyway, this code didn't work in a calculos.c as well.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why not? Show your modified code and the complete error messages.

    Jim

  5. #5
    Registered User
    Join Date
    Nov 2013
    Location
    Curitiba, Brazil, Brazil
    Posts
    16
    Well, I fixed my topic, at least now I've learned what .h are for, but stll need to learn how to use them correctly.
    Above is the code and error, still on array

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It doesn't look like you moved the structure definition into the header file, before the function prototype. Once you do that and #include the header in both main and your other function you should be closer.

    Also in the future please don't alter the code in your previous post, create a new post with the modified source. Changing your previous posts make this topic hard to follow.

    Jim

  7. #7
    Registered User
    Join Date
    Nov 2013
    Location
    Curitiba, Brazil, Brazil
    Posts
    16
    Ok, thank you Jim,I had some problems to figure how to make it work but its done now.
    Thank you a lot for your tips. Thanks to it I can continue with my lessons.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 04-19-2012, 10:46 AM
  2. Header File (Array)
    By ap35896 in forum C Programming
    Replies: 1
    Last Post: 03-28-2012, 03:06 PM
  3. struct with multiple objects in header file issues
    By Darkroman in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2012, 03:00 PM
  4. How can I access a struct (from a header file)?
    By loxslay in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2006, 01:25 PM
  5. Replies: 30
    Last Post: 06-19-2006, 12:35 AM