Thread: Usage of Structs between Various files is not working

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Usage of Structs between Various files is not working

    I have four .c files and 1 .h file. Please bear with me, the problem is only on one line. First a bit of context:

    main.c
    Code:
    #include <stdlib.h>#include <stdio.h>
    #include "headerfile.h"
    
    
    int main(){
        char* postfix;
        char* infixedUserInput = getKeyboardInput();
        postfix = convertToPostfix(infixedUserInput);
        return 0;
    }
    stackUtilities.c
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    #define MAX_STACK_SIZE 100
    #define OPENING_PARENTHESIS (
    
    
    struct NumStack operandStack = {.stackPointer = 0);
    struct CharStack operatorStack = {.stackPointer = 0};
    
    
    void pushNum(struct NumStack *stack, double pushedValue){
        if (stack -> stackPointer < MAX_STACK_SIZE) {
            stack -> stackValue[stack -> stackPointer++] = pushedValue;
        }
        else {
            fprintf(stderr, "Number Stack full, can't push %g\n", pushedValue);
            exit(-1);
        }
    }
    
    
    double popNum(struct NumStack stack){
        if (stack.stackPointer > 0){
            return stack.stackValue[--stack.stackPointer];
        }
        else {
            fprintf(stderr, "Number Stack empty\n");
            exit(-1);
        }
    }
    
    
    void pushChar(struct CharStack *stack, char pushedValue){
        if (stack -> stackPointer < MAX_STACK_SIZE) {
            stack -> stackValue[stack -> stackPointer++] = pushedValue;
        }
        else {
            fprintf(stderr, "Character Stack full, can't push %g\n", pushedValue);
            exit(-1);
        }
    }
    
    
    char popChar(struct CharStack stack){
        if (stack.stackPointer > 0){
            return stack.stackValue[--stack.stackPointer];
        }
        else {
            fprintf(stderr, "Character Stack empty\n");
            exit(-1);
        }
    }
    
    
    void updateStacks(struct NumStack *operandStack, struct CharStack *operatorStack){
        double answer;
        char operation;
        char operatorStackPointer = operatorStack -> stackPointer;
        if (operatorStackPointer > 0){
            while((operation = popChar(*operatorStack)) != 'OPENING_PARENTHESIS'){
                double secondOperand = popNum(*operandStack);
                double firstOperand = popNum(*operandStack);
                switch(operation){
                    case '+':
                        answer = add(firstOperand, secondOperand);
                        break;
                    case '-':
                        answer = subtract(firstOperand, secondOperand);
                        break;
                    case '*':
                        answer = multiply(firstOperand, secondOperand);
                        break;
                    case '/':
                        answer = divide(firstOperand, secondOperand);
                        break;
                }
                pushNum(*operandStack, answer);
            }
        }
    }
    solveArithmeticEquation.c

    Code:
    #include <stdlib.h>#include "headerfile.h"
    
    #define CLOSING_PARENTHESIS )
    
    void convertToPostfix(char *infix){
        char token;
        while(token = getToken(*infix) != '\0'){
                if(token == isdigit(token)){
                    pushNum(&operandStack, atoi(token));
                }
                else if(token != isdigit(token)){
                    pushChar(operatorStack, token);
                }
                else if(token == 'CLOSING_PARENTHESIS'){
                    updateStacks(&operandStack, &operatorStack);
                }
        }
    }

    mathUtilities.c
    Code:
    double add(double firstOperand, double secondOperand){    return firstOperand + secondOperand;
    }
    
    
    double subtract(double firstOperand, double secondOperand){
        return firstOperand - secondOperand;
    }
    
    
    double multiply(double firstOperand, double secondOperand){
        return firstOperand * secondOperand;
    }
    
    
    double divide(double firstOperand, double secondOperand){
        return firstOperand / secondOperand;
    }
    headerFile.h
    Code:
    #define MAX_STACK_SIZE 100
    
    void printCharacterArray(char*);
    char* getKeyboardInput(void);
    char getToken(char **);
    
    
    void pushNum(struct NumStack, double);
    double popNum(struct NumStack);
    void pushChar(struct CharStack , char);
    char popChar(struct CharStack);
    
    
    double add(double, double);
    double subtract(double, double);
    double multiply(double, double);
    double divide(double, double);
    void updateStacks(struct NumStack *, struct CharStack *);
    
    
    struct NumStack{
        double stackValue[MAX_STACK_SIZE];
        int stackPointer;
    };
    
    
    struct CharStack{
        char stackValue[MAX_STACK_SIZE];
        int stackPointer;
    };
    
    
    extern struct NumStack operandStack;
    extern struct CharStack operatorStack;
    EDIT: Error is on line 9 in the solveArithmeticEquation.c

    Compiler is complaining of "type of formal parameter 1 is incomplete"

    What I did do to make it work is by putting this all in one file and then it seems fine. So what seems to be the problem? I have genuinely run out of solutions.
    Last edited by Vespasian_2; 01-19-2017 at 10:46 AM.

  2. #2
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    I started another threa to see if Im declaring my structs correctly across various files and it seems to be fine. I am baffled. PS: Using GNU GCC compiler C99 flag.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    So in other words, if I define NumStackTEST and pushNumTEST in solveArithmeticEquations file instead of the stackUtilities file and then do this for line 9:
    pushNumTEST(&operandStackTEST, atoi(token));
    Then it works. But thats not good enough - I need it in separate files.
    Last edited by Vespasian_2; 01-19-2017 at 10:56 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Compiler is complaining of "type of formal parameter 1 is incomplete"
    It looks like you need to put your structure definitions before the function prototypes that use those structures.

    Code:
    #define MAX_STACK_SIZE 100
     
    struct NumStack{
        double stackValue[MAX_STACK_SIZE];
        int stackPointer;
    };
     
     
    struct CharStack{
        char stackValue[MAX_STACK_SIZE];
        int stackPointer;
    };
     
    
     
    void printCharacterArray(char*);
    char* getKeyboardInput(void);
    char getToken(char **);
     
     
    void pushNum(struct NumStack, double);
    double popNum(struct NumStack);
    void pushChar(struct CharStack , char);
    char popChar(struct CharStack);
     
     
    double add(double, double);
    double subtract(double, double);
    double multiply(double, double);
    double divide(double, double);
    void updateStacks(struct NumStack *, struct CharStack *);
     
     
    extern struct NumStack operandStack;
    extern struct CharStack operatorStack;
    Also you need to add include guards to your header.


    Jim

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Now its saying incompatible type for argument 1...

  6. #6
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Ah wait its because I didnt update something else in the header file. Hang on. But I think you led me to the right track jim!

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Your code is producing an alarming number of warnings and errors for the amount of code written.

    You should be compiling and testing often.

    A development process

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Now its saying incompatible type for argument 1...
    Where? What line? What file? What function?

    Post the complete error message exactly as it appears in your development environment.


    Jim

  9. #9
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Where? What line? What file? What function?

    Post the complete error message exactly as it appears in your development environment.


    Jim
    My code is hopelessly broken. As soon as I fix something another error pops up. I will be wasting your time if I asked for help. Let me start on a clean slate and test iteratively as Matticus says. I'll come back when I get a solid foundation built.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well while your working on your program strive to stay away from global variables learn to properly pass the variables to and from the functions that require them.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio 2015 — CPU Usage Profiling Not Working?
    By BobLoblaw465634 in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2015, 09:46 PM
  2. mktmp() and mkstmp() files and usage
    By heatblazer in forum C Programming
    Replies: 0
    Last Post: 07-03-2015, 06:28 AM
  3. Vector os structs not working how it is supposed to...
    By Gabriel Chamon in forum C Programming
    Replies: 5
    Last Post: 06-16-2012, 11:31 PM
  4. Structs vs. Classes, Memory Usage
    By CrazyNorman in forum Game Programming
    Replies: 2
    Last Post: 07-17-2005, 05:43 PM
  5. header files usage
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-20-2002, 01:01 AM

Tags for this Thread