如何通过指针数组重申?

How do I reiterate through a pointer array?

本文关键字:数组 何通过 指针      更新时间:2023-10-16

我的程序应该只使用指针遍历数组的次数与小时数一样多。

for (int i = 0; i < totalHours; i++)
{
cout << " " << i + 1;
while (ptrSvr <= last)
{
rand_set(ptrSvr);
print(ptrSvr);
ptrSvr++;
}
cout << endl;
}
return 0;

这是rand_set的功能

void rand_set(int* &ps)
{
*ps = rand() % 10;
}

这就是打印功能。

void print(int* ps)
{
cout << "       " << *ps << " ";
}

但是,一旦迭代,我不明白如何将指针设置回数组中的第一个地址,以便当我增加时,它将从开头开始打印一个新行。目前,它只会打印一行。还有一个问题 - 我只能使用给定的指针来访问数组。我不能使用数组索引。

此外,随机生成的最大数字必须存储在 highestTry 指针中,并且在打印时,旁边必须有一个 *。此功能必须包含在rand_set函数中。我只让它在所有这些旁边打印一个 *,或者它被打印在第一个随机数上,如果它们增加,则打印在每个随机数上。

提前感谢您的任何建议。我在一个 200 级的 c++ 课程中,所以这应该尽可能简单。

变量是这样声明和初始化的。

void initialize(int ts, int* &ps, int* &pt,
int* &l, int* &ha)
{
for (int i = 0; i < ts; i++)
{
ps[i] = 0;
pt[i] = 0;
l = &ps[i];
}
ha = NULL;
srand(time(NULL));
}

// Pointer declarations          
int *ptrSvr;                // Po
int *ptrTotal;              // Po
int *last;                  // Po
int *highestAttempts;       // Po
// Variable declarations         
int totalServers;           // Va
int totalHours;             // Va
int total;                  // Va
// Prompt user for number of serv
// totalServers and totalHours   
cout << "Enter the number of web 
cin >> totalServers;             
cout << "Enter the number of hour
cin >> totalHours;               
// Declares arrays for ptrSvr and
// of total servers              
ptrSvr = new int[totalServers];  
ptrTotal = new int[totalServers];

如果你有类似的东西

int arr [] = {};

然后

ptrSvr = arr;

while否则,您必须存储ptrSvr的第一个值,并在循环退出后重置while