如何更改函数中的字符串数组
How to change a string array within a function
我在更改字符串数组的值时遇到问题。代码的重点是检查它是什么故事编号,然后更改字符串数组的值,然后通过forloop在choices函数中打印出来。
void CheckDecisions(std::string choices[], int num_choices, int story_number)
{
//choices = new std::string[];
const int num = 4;
num_choices = num;
if (story_number == 0)
{
std::string start_choices[num] = { "", "Home", "The Store", "Your friend Taylor's place" };
choices = start_choices;
}
if (story_number == 1)
{
//std::string_choices[num_choices] = { "", "Hummana", "Hummana", "Yes!" };
}
}
它不会更改字符串的值,并且仍然返回空值。正确的语法是什么?
这是整个代码:
#include <iostream>
#include <string>
#include <vector>
void CheckStoryArk(int choice, std::string story, int story_number)
{
if (story_number == 0)
{
if (story == "Home")
{
printf("story is Homen");
}
if (story == "The Store")
{
printf("Story is Storen");
}
if (story == "Your friend Taylor's place")
{
printf("Story is Taylorn");
}
}
if (story_number == 1)
{
if (story == "whatever")
{
printf("something");
}
}
if (story_number == 2)
{
printf("story number is 3n");
}
if (story_number == 3)
{
printf("story number is the endn");
}
}
void CheckDecisions(std::string choices[], int num_choices, int story_number)
{
//choices = new std::string[];
const int num = 4;
num_choices = num;
if (story_number == 0)
{
std::string start_choices[num] = { "", "Home", "The Store", "Your friend Taylor's place" };
choices = start_choices;
}
if (story_number == 1)
{
//std::string_choices[num_choices] = { "", "Hummana", "Hummana", "Yes!" };
}
}
//Prints out choices
void Choices(int num_choices, std::string choices[], int choice, std::string story, int story_number)
{
CheckDecisions(choices, num_choices, story_number);
for (int i = 1; i < num_choices; i++)
{
//printf("%d %sn", i, choices[i]);
std::cout << i << " " << choices[i] << "n";
}
std::cin >> choice;
if(choice >= num_choices)
{
printf("That's not a valid answer, dickbag!n");
std::cin >> choice;
}
story = choices[choice];
CheckStoryArk(choice, story, story_number);
}
void Story(int story_number, const int num_choices, std::string choices[], int choice)
{
printf("You're driving down the streets of Los Angeles, where are you going?n");
for (int i = 0; i < story_number; i++)
{
Choices(num_choices, choices, choice, choices[choice], i);
}
}
void main()
{
const int num_choices = 4;
int story_number = 4;
int choice = 0;
std::string choices[num_choices] = {};
Story(story_number, num_choices, choices, choice);
}
问题不是您的语法,而是您调用了未定义的行为。
std::string start_choices[num] = { "", "Home", "The Store", "Your friend Taylor's place" };
choices = start_choices;
当函数存在时,start_choices
数组将被销毁,因此将其分配给choices
并尝试在函数之外访问它是未定义的。
相反,将数据复制到choices
阵列中:
std::copy (std::begin(start_choices), std::end(start_choices), choices);
或者只使用std::vector
,完全避免头痛。
相关文章:
- 将C#字符串数组传递给C++
- 如何为 C 型字符串数组编写 getter 和 setter?
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 尝试将 c 字符串数组与分隔符连接起来
- 将字符串数组传递给接受常量字符**的函数
- 返回 C++ 中的字符串数组
- 如何从COM模块中的函数返回字符串数组?
- 无法将字符串数组声明为类成员而不是字符 (C++)
- 删除字符串数组
- 如何将字符串数组返回到 java JNI
- 将字符串数组作为函数参数传递
- C++将字符串数组的元素存储到变量中
- 循环访问还包含未使用元素的字符串数组
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 乘以字符串/数组和全局数组
- 递归二进制搜索与字符串数组
- 如何初始化一个标准::字符串数组?
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- C++字符串数组的动态向量
- 给定一个等长字符串数组