简体   繁体   English

如何将char类型数组传递给函数,并将第一个char数组复制到第二个图表数组,并使用c ++比较它?

[英]How to pass char type array to a function and copy 1st char array to 2nd chart array and compare it using c++?

Here is what I want: 这是我想要的:
Declare a character array of size 15 to store character (string input) value from the user. 声明一个大小为15的字符数组,以存储用户的字符(字符串输入)值。 Now perform the following tasks: 现在执行以下任务:

  • Pass the array to function copy(). 将数组传递给函数copy()。
  • Define another array of the same size in above function. 在上面的函数中定义另一个相同大小的数组。 Copy the values of the first array to the second array and display on the console. 将第一个数组的值复制到第二个数组并在控制台上显示。
  • From function Copy() pass both arrays to function compare(). 从函数Copy()中将两个数组都传递给函数compare()。 At that function compare both arrays and display message "Equal" if the condition is fulfilled. 在该函数处,比较两个数组,如果满足条件,则显示消息“等于”。

Here is my code 这是我的代码

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;

void mycopy(char array);
int main(){
//Using Loop to input an Array from user

char array[15];
int i;
cout << "Please Enter your 15 characters" << endl;
cout << "**************************************************" << endl;
for (i = 0; i < 15; i++)
{
    cin >> array[i];
}

// output each array element's value
cout << "Please Enter your 15 characters" << endl;
cout << "**************************************************" << endl;
cout << "Element" << setw(13) << "Value" << endl;
for (int j = 0; j < 15; j++) {
    cout << setw(7) << j << setw(13) << array[j] << endl;
}

mycopy(array[15]);

return 0;
}

void mycopy(char array[15]) {

char array1[15];
strncpy_s(array1, array, 15);
cout << "The output of the copied Array" << endl;
cout << "**************************************************" << endl;
cout << "Element" << setw(13) << "Value" << endl;
for (int j = 0; j < 15; j++) {
    cout << setw(7) << j << setw(13) << array1[j] << endl;
}

} }

The above code is to pass the array to function Copy() and copy the values of the 1st array to the 2nd char array but the code generates an exception due to the invalid parameter is passed. 上面的代码将数组传递给函数Copy()并将第一个数组的值复制到第二个char数组,但是由于传递了无效参数,该代码生成了异常。 As I have searched the stack overflow but I didn't find any similar question that could solve my problem. 当我搜索堆栈溢出时,但没有找到任何类似的问题可以解决我的问题。 Thanks in advance. 提前致谢。

Do not use strncpy_s , it is non-standard. 不要使用strncpy_s ,这是非标准的。 Instead, use strncpy as you were originally. 相反,请像原来一样使用strncpy In order to use it, you need to include cstring . 为了使用它,您需要包括cstring

#include <cstring>

Your prototype and definition for mycopy() are different. 您的原型和mycopy()定义是不同的。 Your prototype takes a char but your definition takes a char array. 您的原型需要一个char但是您的定义需要一个char数组。 Make them both take an array. 使它们都采用数组。 Either of the following three will work the same: 以下三个中的任何一个都将起作用:

void mycopy(char* array);
void mycopy(char array[]);
void mycopy(char array[15]);

When you are calling mycopy() in main() you are attempting to access the array at the 15th index and pass that character to the function. 当您在main()中调用mycopy() ,您尝试访问第15个索引处的数组并将该字符传递给函数。 This is wrong because the 15th index is out of bounds and because the function takes a pointer to a char array, not a char . 这是错误的,因为第15个索引超出范围,并且该函数采用了指向char数组而不是char的指针。 You need to just pass the pointer to the array. 您只需要将指针传递给数组即可。

mycopy(array);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM