【发布时间】:2023-03-17 15:54:01
【问题描述】:
我有一个字符串数组,我从一个函数传递到我的主函数中,我想将该数组更改为一个常量,一旦它位于主函数中,以便其他函数无法操作它。我不知道该怎么做。我是一名学生,不能为这项作业使用指针。这是我所拥有的:
//declare variables
string name;
const int size = 11;
string ans[size];
const string corrAns[size] = { "C++","for","if","variable","function", "return",
"array","void","reference","main", "prototype" };
const string studAns[size];
double percent;
// read file
readFile(name, ans, size);
// calculate percentage
percent = calculatePercentage(corrAns, studAns, size);
// print out report
printReport(name, percent, corrAns, studAns, size);
system("Pause");
return 0;
}
程序的其余部分按我想要的方式工作,但是我不确定我应该如何有效地将ans 转移到studAns,并且在任何地方都没有成功找到答案。
【问题讨论】:
-
使用
const std::string*作为函数参数的类型。 -
为什么你认为你需要将
ans转移到任何地方而不是直接使用它? -
我作为一名专业程序员工作了很多年,但我从未编写过代码来在
main中间构造一个 const 数组。你不能使用指针...你可以使用引用吗? -
我可以使用引用而不是指针。我们只是在学习指针,这应该使用我们已经介绍过的东西。
标签: c++ arrays constants data-transfer