指向函数的结构指针

Structure pointer to function?

本文关键字:结构 指针 函数      更新时间:2023-10-16

我试图将我在一个函数中创建的结构传递给另一个函数,所以基本上我要做的是动态创建所需结构的数量,结构的数量是在一个文本文件中,所以在文本文件中会有像5和5这样的数字数据集。我想把我在函数中创建的结构传递给另一个函数。我几个月前开始编程,所以如果有一个简单的解决方案,或者如果这个问题已经被问到,请原谅我。

struct graph
{
int Max,Min,index;
double dataArray[300];
};
void readfile()
{
int amount;
char tmpSTR,nmbrGraph;
ifstream myFile("data1.txt",ios::in);
myFile>>amount;
myFile>>tmpSTR;
myFile>>nmbrGraph;
graph* Data = new graph[amount];
    for(int j=0;j<nmbrGraph;j++)
    {
    for(int i=0;i<299;i++)
        myFile>>Data[j].dataArray[i];
    }
//hOW WOULD I PASS THE STRUCTURE "DATA" TO THE FUNCTION anotherFunction?
}
void anotherFunction()
{
for(int i = 0;i<300;i++)
cout<<Data[scroll].dataArray[i])<<endl; /*Error here! scroll being an 
integer declared globally*/
}

通过值或引用将graph*指针作为参数传递给anotherFunction。此外,包含数字项也很重要,因为这是通过读取文件确定的,并且事先不知道。

// by value
void anotherFunction(graph* Data, int amount);
// by reference
void anotherFunction(graph*& Data, int amount);