关于函数strcpy_s
about the function strcpy_s
我想输出三个国家的第一个字母的最小字母,这是代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
void smallest_string(char str[][30], int);
int i;
char country_name[3][30];
for (i = 0; i < 3; i++)
cin >> country_name[i];
smallest_string(country_name, 3);
system("pause");
}
void smallest_string(char str[][30], int n)
{
int i;
char string[30];
***strcpy_s(string, str[0]);***
for (i = 0; i < n; i++)
if (strcmp(str[i], string) < 0)
strcpy_s(string, str[i]);
cout << endl << "the smallest string is:" << string << endl;
}
在此代码strcpy_s(string, str[0]);
中,似乎可以删除。为什么?
如果您希望循环从i = 1
开始,则行strcpy_s(string, str[0]);
是必不可少的。
但是,由于您从i = 0
启动它,因此如果您还定义以下启动条件,则并不严格要求它:string[0] == CHAR_MAX
和string[1] == 0
。但如果不是这种情况,那么您将体验到未定义的行为。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++ strcpy 函数在少数主要情况下失败
- 在函数 strcpy() 中访问字符数组时出现分段错误
- 如何修复编译错误"此函数或变量可能不安全"(strcpy)
- 如何在C++中实现 strcpy 函数
- 这段代码的输出是什么,它在构造函数中使用 strcpy?
- strcpy 对指针函数的引用
- 使用 strcpy 时构造函数中的分段错误
- C++:strcpy 函数复制空值
- C++传递给函数的 strcpy 指针
- c++控制台程序中strcpy函数的异常.strcpy()中的
- 函数 strcpy 不起作用
- 不了解示例中的行为 - strcpy() 和返回本地数组地址的函数
- 这个带有指针函数的 strcpy 有什么问题?