简体   繁体   中英

How to store specific elements of Struct into pointer

Ok, so my pointer logic is a little flawed, but I'm working on it. My problem is in the main.cpp file below, Inside the getStructData() function. I have the questions listed down there in comments, and what I think seems right, but know it's not. I will now put the question up here, out of the comments.

I have a function getMyStructData(), I can currently print out the elements of a specific struct based on an index number. Instead I'd like to copy the elements of that struct at the given index number (int structureArrayIndex) from the private structure into the structure of the pointer argument.

Inside myStructure.h

struct myStructure
{
    int myInteger;
    double myDoublesArray[5];
    char myCharArray[80];

};

Inside myClass.h

#include "myStructure.h"

class myClass
{
private:
    myStructure myStruct[5]

private:
    Prog1Class();
    ~Prog1Class();
    void setMyStructData();
    void getMyStructData(int structureArrayIndex, struct myStructure *info);
};

Inside main.cpp

#include<iostream>
#include <string>
#include "myClass.h"
#include "myStructure.h"

using namespace std;

void myClass::setMyStructData()
{
    for(int i = 0; i < 5 ; i++)
    {
        cout << "Please enter an integer: " << endl;
        cin >> myStruct[i].myInteger;

        for(int j = 0; j< 5; j++)
        {
            cout << "Please enter a double: ";
            cin >> myStruct[i].myDoublesArray[j];
        }

        cout << endl << "Please enter a string: ";
        cin.ignore(256, '\n');
        cin.getline(myStruct[i].myCharArray, 80, '\n');
    }
}

void Prog1Class::getStructData(int structureArrayIndex, struct myStructure *info)
{
//****Below I have what's working, but Instead of just printing out the elements, what I want to do is copy the elements of that struct at the given index number (int structureArrayIndex) from that private structure into the structure of the pointer argument.  

//I'm guessing something like this:
// info = &myStructure[structureArrayIndex];
//I know that's wrong, but that's where I'm stuck.  

//****Here is how I would print out all of the data using the int structureArrayIndex
cout << myStruct[structureArrayIndex].myInteger << endl;
for (int k = 0; k < 5; k++)
{
    cout << myStruct[structureArrayIndex].myDoublesArray[k] << endl;
}
cout << myStruct[structureArrayIndex].myCharArray << endl;

}

int main(void)
{
    myClass c;
    c.setMyStructData();
    c.getStructData(1);

    cin.get();
}

In your commented code, you are assigning pointers and not an actual copy of the data.

To do what you ask with the code you provided you may do:

  // Check that info isn't null
  if (!info) {
    return;
  }
  // Simple copy assignment of private structure to info.
  *info = myStruct[structureArrayIndex];

This dereferences the pointer info and does a default copy operation of the myStructure type in myStruct array at the structureArrayIndex.

您必须将myStruct[structureArrayIndex]的内容分配给info内容。

*info = myStruct[structureArrayIndex];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM