如何通过指针数组重申?
How do I reiterate through a pointer array?
我的程序应该只使用指针遍历数组的次数与小时数一样多。
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
相关文章:
- 通过指向指针数组的指针访问子类的属性
- 通过JNI传递数据数组的最快方法是什么
- 通过交换元素使数组相同
- 通过指针与数组引用扩展数组索引序列
- 通过unique_ptr访问std::数组
- 当我尝试通过 arduino 上的引用传递布尔数组时出现问题
- 通过指针算法计算数组长度
- 如何在 c# 代码中通过引用调用时从 c++ dll 更新数组值?
- 在C++中,建议通过数组循环的方式是什么?
- 当我尝试通过构造函数分配 char 数组时出现错误
- 通过分隔符分隔包含 UTF-16 BE 文本的uint8_t数组
- 如何通过 malloc 为队列数组分配内存?
- 我正在尝试修改一个字节数组并通过 JNI 将其从 c++ 发送到 java
- 数组是通过引用传递的,但是如果我只传递未存储在内存中的数组的值,会发生什么
- C 数组并未通过参考通过
- 为什么数组只能通过引用返回
- 包含"random"值的类的数组属性(通过另一个类的指针访问时)
- 结构数组-不通过数据的函数
- 在2D数组中通过重载数组下标操作符进行访问
- C++字符数组未通过管道读取