CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2010
    Posts
    8

    How do you pass a struct with an array member?

    How do you pass a struct with an array member to another function? This is what i have so far.
    People are telling me to use an array container ([] or *) but I have no idea how to implement this in my situation.

    Code:
    #include <iostream>                 
    using namespace std;
    
    int edit(struct Student data);
    struct Student{
     int number;
     char name[10];
     double money;
    };
    
    int main(){
     struct Student info[2];
     int i;
    for(i=0;i<2;i++){
      cin >> info[i].number >> info[i].name >> info[i].money;
     }
     i=0;
    while(i < 2){
      edit(info);
    }
     
     cout << info[1].number << info[1].name << info[1].money << endl;
    
    return 0;
    }
    
    int edit(struct Student data){
     data[0].number = 666;
     data[0].name = "TEST";
     data[0].money = 99.99;
    cout << data[0].number << data[0].name << data[0].money << endl;
    
    return 0;
    }

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: How do you pass a struct with an array member?

    int edit(struct Student& data)
    {
    }

    If you are working with C:

    int edit(struct Student* data)
    {
    }

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: How do you pass a struct with an array member?

    Quote Originally Posted by djgirotto View Post
    How do you pass a struct with an array member to another function? This is what i have so far.
    Well, what you have is fine in principle, though it may not do what you want. You are passing a Student object to the function edit by value. That means that a copy of the object is made and the function works on the copy. If you want the function to work on the original object, you'll have to pass it by reference (or const reference if you don't want to alter the object). E.g.
    Code:
    void Init(Student& data)
    {
        data.number = 0;
        data.name[0] = '\0';
        data.money = 0.;
    }
    void Display(const Student& data)
    {
        std::cout << data.number << ' ' << data.name << ' ' << data.money << '\n';
    }
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Feb 2010
    Posts
    8

    Re: How do you pass a struct with an array member?

    Does this make sense?
    Code:
    #include <iostream>
    using namespace std;
    
    void edit(Student *data);
    
    struct Student{
     int no;
     char name[10];
     double money;
    }
    
    int main(){
     struct Student data[2];
     
     data[0].no = 5;
     data[0].name = "Mike";
     data[0].money = 10.50;
    
     edit(Student &data);
    
     cout << data[0].no << data[0].name << data[0].money << endl;
     cout << data[1].no << data[1].name << data[1].money << endl;
    
     return 0;
    }
    
    void edit(Student *data){
     *data[1].no = 10;
     *data[1].name = "Test";
     *data[1].money = 99.99;
    }
    Is this possible?

    Expecting it to print out...

    >5Mike10.50
    >10Test99.99
    >
    Last edited by djgirotto; February 21st, 2010 at 03:23 PM.

  5. #5
    Join Date
    Feb 2005
    Posts
    2,160

    Re: How do you pass a struct with an array member?

    I would think that this:

    Code:
      data[0].name = "Mike";
    Wouldn't even compile. There is no = operator for a char array. Try strcpy or use a std::string.

  6. #6
    Join Date
    Feb 2010
    Posts
    8

    Re: How do you pass a struct with an array member?

    I am still unclear as to how I would pass an array of structs to functions. Could anyone provide me with some assistance?

    Also.. if I want to compare user's integer input, if they input a value and if I want to test if it's 0 or not how would I do that?
    Last edited by djgirotto; February 22nd, 2010 at 12:34 AM.

  7. #7
    Join Date
    Jul 2002
    Posts
    2,543

    Re: How do you pass a struct with an array member?

    Code:
    void edit(Student *data){
     (*data)[1].no = 10;
     // ...
    }

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: How do you pass a struct with an array member?

    Quote Originally Posted by djgirotto View Post
    Does this make sense?
    Is this possible?

    Expecting it to print out...

    >5Mike10.50
    >10Test99.99
    >
    Most people used to coding in C++ would not use arrays anyway, but an STL containers instead.
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    struct Student{
     int no;
     string name;
     double money;
    };
    
    void edit(vector<Student> &data);
    
    int main(){
     vector<Student> data(2);
     
     data[0].no = 5;
     data[0].name = "Mike";
     data[0].money = 10.50;
    
     edit(data);
    
     cout << data[0].no << data[0].name << data[0].money << endl;
     cout << data[1].no << data[1].name << data[1].money << endl;
    
     return 0;
    }
    
    void edit(vector<Student> &data){
     data[1].no = 10;
     data[1].name = "Test";
     data[1].money = 99.99;
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  9. #9
    Join Date
    Feb 2010
    Posts
    8

    Re: How do you pass a struct with an array member?

    Unfortunately the project I'm working on has a set requirement which states we must use arrays, don't know anything about vectors at the moment.

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How do you pass a struct with an array member?

    When passing any sort of array to a function, you should always pass the length of the array as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured