Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8977449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:22:27+00:00 2026-06-15T19:22:27+00:00

From what I understand, passing an entire structure to a function and not doing

  • 0

From what I understand, passing an entire structure to a function and not doing so by reference can be taxing on system resources. So, I’d like to get a handle on passing them by reference. I’m creating a character generator with the following structures:

struct Stats{
    string name;
    int level;
    int HP;
    int STR;
    int CON;
    int DEX;
    int INT;
    int WIS;
    int CHA;};

struct Growth{
    int HPperlvl;
    int STRperlvl;
    int CONperlvl;
    int DEXperlvl;
    int INTperlvl;
    int WISperlvl;
    int CHAperlvl;};

struct Holdstats{
    Stats classstats;
    Growth classgrowth;};
const int SIZE = 10;

Holdstats classlist[SIZE] = {
    { {"Fighter", 1, 18, 10, 10, 10, 10, 10, 10}, {1,1,1,1,1,1,1} },
    { {"Wizard", 1, 10, 10, 10, 10, 10, 10}, {1,1,1,1,1,1,1}},
    { {"Rogue", 1, 10, 10, 10, 10, 10, 10}, {1,1,1,1,1,1,1}}
} //These are just some examples, will be filled later

Holdstats charlist[SIZE]; //Empty array to store created characters

I’ll use the simplest function in the program to illustrate my problem. The following function is simply supposed to display info for whatever structure is passed to it, at whatever position in the structure is passed to it. The position is previously defined in the program. I would like to pass both the struct and the position by reference.

void dispinfo(const Holdstats &p, int &i) //Should be passed a position and a structure
{
    cout << endl << "\tHere is the Character/Class info you requested: "
        << "\n\t----------------------------------------------"
        << "\nName:\t\t" << p[i].classstats.name << endl
        << "Level:\t\t" << p[i].classstats.level << endl
        << "Hit Points:\t\t" << p[i].classstats.HP << endl
        << "Strength:\t\t" << p[i].classstats.STR << endl
        << "Constitution\t\t" << p[i].classstats.CON << endl
        << "Dexterity\t\t" << p[i].classstats.DEX << endl
        << "Intelligence\t\t" << p[i].classstats.INT << endl
        << "Wisdom\t\t" << p[i].classstats.WIS << endl
        << "Charisma\t\t" << p[i].classstats.CHA << endl;

}

The issue I’m getting has to do with p[i]. My compiler is telling me that for
Holdstats &p – no operator [] matches these operands. I would just use the name of a previously defined structure array (“classlist” or something) in the function header, but I want to be able to pass any array of structures to the function.

To my knowledge, I’m calling element i of a previously defined array. But obviously, I’m doing something wrong. Can anyone see the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:22:29+00:00Added an answer on June 15, 2026 at 7:22 pm

    A few thoughts to try and help out.

    • Unless you’ll be changing the value of i within dispinfo(), there’s really no reason to pass it by reference. Passing an int and passing a reference to an int is probably equivalent (both 32-bits more than likely) but then accessing that int by reference is (trivially) more taxing since it has to be dereferenced to get at the value.
    • It looks like you’re expecting an array of Holdstats structs to get passed into dispinfo(). If that’s the case, you don’t need the reference symbol as arrays are always passed by reference. You should use Holdstats p[] instead of const Holdstats &p and the array would get passed in just fine (and you could compile). To call dispinfo() you’d just pass the array name and the index you want to print out.

    You may want to consider changing dispinfo() to only receive a single Holdstats struct rather than an array of them. Then the function would look something like this.

    void dispinfo(const Holdstats &p) //Should be passed a structure only
    {
        cout << endl << "\tHere is the Character/Class info you requested: "
            << "\n\t----------------------------------------------"
            << "\nName:\t\t" << p.classstats.name << endl
            << "Level:\t\t" << p.classstats.level << endl
            << "Hit Points:\t\t" << p.classstats.HP << endl
            << "Strength:\t\t" << p.classstats.STR << endl
            << "Constitution\t\t" << p.classstats.CON << endl
            << "Dexterity\t\t" << p.classstats.DEX << endl
            << "Intelligence\t\t" << p.classstats.INT << endl
            << "Wisdom\t\t" << p.classstats.WIS << endl
            << "Charisma\t\t" << p.classstats.CHA << endl;
    
    }
    

    Then your call to dispinfo() would look something like this.

    Holdstats classlist[SIZE] = { [...] };
    dispinfo(classlist[0]);
    dispinfo(classlist[1]);
    [...]
    

    That’s what you’d effectively be doing by passing the array and an index like so: dispinfo(classlist,0);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As you can understand from title, i modified Settings.cs file.Added some properties, some code
From what I understand, the current method of doing integer division is calculating the
From what I understand of REST principles, URLs should represent a single resource, like
What I have understand about passing arguments to main() from command line is that
I'm new to Scala, and from what I understand yield in Scala is not
I have not been able to get model binding to work when doing a
I understand from bunch of other Stackoverflow threads (like this ) that Template arguments
I have a question about the map function in Python. From what I understand,
I am having trouble passing data from one activity to another. I can easily
I just don't understand why i can't call an object like this. <?php $obj

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.