Trouble passing arguments to a function - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Trouble passing arguments to a function

dashchandandashchandan Posts: 8Member
Hi Everyone,

I have been writting a program where, i have defined the following structure in a header file :
struct the_box
{
int final_value;
int array[10];
};

I have another header file in which i have defined some functions, the main one being :

phase1(struct the_box obj);

In my main program, i have created an object for struct the_box and i have passed it to phase1.

This is giving me lots of errors, can someone please tell me whats going wrong here? How can i pass a user-defined structure to a function when they are in different header files? I have included all the user-derfined header files in both the main program and in the header file containing the functions.

Thanks in advance for any help

Comments

  • freedom_docfreedom_doc Posts: 17Member
    You said "how can I pass a struct to a function when they are defined in different headers"?

    The answer is to include the header file with the struct in the header file where you have your code that calls for the struct. Indeed, where ever it is that you mention the struct, that code needs to #include the header that has the struct.

    Are you using C or C++ ? (understand these are DIFFERENT languages).
    Also if the above doesn't fix your problem we will need to see more code.
  • dashchandandashchandan Posts: 8Member
    Yes, i think the problem is that i am doing something wrong in the argument passing of the function. I did include the header file containing the structure in the header file containing the function. The program is written in C.

    The header file with the structure is :

    struct the_box
    {
    int final_value;
    int array[9];
    };

    The header file with the function is :

    #include "the_box.h"

    int phase1(struct the_box obj[9][])
    {
    int i,j;
    for(i=0;i<9;i++)
    for(j=0;j<9;j++)
    printf("%d",obj[i][j].final_value);

    return 0;
    }

    The main program :

    #include "the_box.h"
    #include "phases.h"

    int main()
    {
    struct the_box obj[9][9];
    /*
    some input stuff goes here
    */

    phases(obj);
    return 0;
    }

    I hope this code helps. Can you please tell me what could be wrong? I am using a GCC compiler. It gives me an error saying "redefinition of struct the_box".

    I also tried using the keyword extern and defined the object of the structure globally, that does not seem to help either
  • freedom_docfreedom_doc Posts: 17Member
    I think the main problem here is your use of array dimensions with 9 the first and blank for the second. IN C you need not specify the first dimension to an array parameter BUT you need all the rest! Reverse these.
  • dashchandandashchandan Posts: 8Member
    i tried reversing the values, unfortunately that didn't help much. I am still getting the same error message.
  • LundinLundin Posts: 3,711Member
    Perhaps you meant to do something like this:

    [code]/* h file */

    typedef struct the_box
    {
    int final_value;
    int array[9];
    } Box;


    void phase1(Box obj[][9]);


    /* c file */

    void phase1(Box obj[][9])
    {
    int i,j;

    for(i=0;i<9;i++)
    {
    for(j=0;j<9;j++)
    {
    printf("%d ", obj[i][j].final_value);
    }
    printf("
    ");
    }
    }


    /* main */

    int main()
    {
    Box obj[9][9];

    phase1(obj);

    return 0;
    }[/code]
  • dashchandandashchandan Posts: 8Member
    Hi everyone,

    Thanks for all the help. I found out what was wrong in my program.

    If i have a structure in a header file like below :
    /* header1.h */

    struct A
    {
    int abc;
    };

    and i have another header file which has a function like so :

    /* header2.h */
    #include "header1.h"

    int phase(struct A obj)
    {
    obj.A = 34;
    return 0;
    }

    then in my main, i only have to include the header file header2.h as this will invoke header1.h on its own.

    If you include header1.h and header2.h in the main, it will give multiple struct definition. Thanks for the help.

  • dashchandandashchandan Posts: 8Member
    Hi everyone,

    Thanks for all the help. I found out what was wrong in my program.

    If i have a structure in a header file like below :
    /* header1.h */

    struct A
    {
    int abc;
    };

    and i have another header file which has a function like so :

    /* header2.h */
    #include "header1.h"

    int phase(struct A obj)
    {
    obj.A = 34;
    return 0;
    }

    then in my main, i only have to include the header file header2.h as this will invoke header1.h on its own.

    If you include header1.h and header2.h in the main, it will give multiple struct definition. Thanks for the help.

Sign In or Register to comment.