Menu

passing structs to functions

2002-11-06
2012-09-26
  • Nobody/Anonymous

    I have an array of structs, example:
    struct mystruct
    {
    int blah;
    double blahblah;
    };

    mystruct mylist[10];

    void myfunction(mystruct mylist[]);
    {
    //function
    }

    it gives me an error at the '{' after void. what is wrong with my code? sorry for not giving a good example.

     
    • Nobody/Anonymous

      Remove that semicolon at the end of:

      void myfunction(mystruct mylist[]);

      Derek

       
    • Nobody/Anonymous

      Let me take a look at my book when I get to work, but I thought the notation for using an array as a function argument was something like

      void myfunction(mystruct mylist[], int n)

      where n is the number of elements in the array.   But it is early in the morning and I am pretty dim to start with.

      The earlier note on teh semicolon is also correct, you have done the classic error of creating an empty function, i.e.

      void myfunction(mystruct mylist[]);
                                                         ^
      This is your function, the nothing between ) and ;

      Common mistake.  Done it a lot, will do it some more.

      Wayne

       
    • j@ck_

      j@ck_ - 2002-11-09

      Hello,
      I have this simple program to offer. Hope it can help.

      #include <cstdlib>
      #include <iomanip> //..setprecision ect
      #include <iostream>

         using namespace std;

      struct alphaDom
      {
        double st_cap;
        double st_han;
        double st_kop;

        double networkNumber();                 
      };

      int main()
      {
        cout.setf(ios::fixed);
       
        alphaDom NalphaDom = { 103.498, 206.998, 302.986 };
         alphaDom* palphaDom = &NalphaDom;                    
          cout << endl
             << "networkNumber is " << setprecision(3) <<palphaDom->networkNumber() << endl << endl;

        system("PAUSE");
        return 0;
      }

      double alphaDom::networkNumber()
      {
        return st_cap * st_han * st_kop;
      }

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.