简体   繁体   English

C ++ char数组传递给函数

[英]C++ array of char pass to functions

Hey guys, I need help with this one. 嘿伙计们,我需要帮助。 I'm writing a really basic program that will generate a character array firstName[amount][size]. 我正在写一个真正的基本程序,它将生成一个字符数组firstName [amount] [size]。 using this it will then pass it to the function addPerson and that will modify the value. 使用它,然后将它传递给函数addPerson,这将修改该值。

Here's my current code 这是我目前的代码

void deletePerson(int &, int, char[], char[], char[], char[]);
int main()
{
char firstNames[100][25], lastNames[100][25], email[100][50], phone[100][16];
else if (userChoice == 'D')
    {
        deletePerson(count, amount, &firstNames,&lastNames,&email,&phone);
    }
}
void deletePerson(int count, int amount, firstNames&, lastNames&,email&,phone&);
{
//blahblahblah
}

I know that it's wrong and pardon my newbieness. 我知道这是错误的,请原谅我。 I had it working to a point where it only had on error but then i decided to change everything. 我让它工作到只有错误的地步,但后来我决定更改所有内容。 Anyway, I hope you get the point of what i'm trying to say. 无论如何,我希望你能理解我要说的话。 Basically i Need to pass the array and have it modified inside the function. 基本上我需要传递数组并在函数内部对其进行修改。 How might i go about that? 我怎么可能这样做?

Thanks! 谢谢!

edit: Ok so i can't use global variables either, that'd be too easy. 编辑:好的,所以我也不能使用全局变量,这太容易了。

I edited the code to get rid of the &. 我编辑了代码以摆脱&。 it gives me 它给了我

error LNK2019: unresolved external symbol "void __cdecl deletePerson(int &,int,char (* const)[25],char (* const)[25],char (* const)[50],char (* const)[16])" (?deletePerson@@YAXAAHHQAY0BJ@D1QAY0DC@DQAY0BA@D@Z) referenced in function _main 错误LNK2019:无法解析的外部符号“ void __cdecl deletePerson(int&,int,char(* const)[25],char(* const)[25],char(* const)[50],char(* const)[16 ])函数_main中引用的“(deletePerson @@ YAXAAHHQAY0BJ @D1QAY0DC @ DQAY0BA @ D @ Z”)

my new code looks like 我的新代码看起来像

void deletePerson(int &, int, char[][25], char[][25], char[][50], char[][16]);
int main()
{
char firstNames[100][25], lastNames[100][25], email[100][50], phone[100][16];
else if (userChoice == 'D')
        {
            deletePerson(count, amount, firstNames,lastNames,email,phone);
        }
}
void deletePerson(int count, int amount, char firstNames[100][25], char lastNames[100][25],char email[100][50],char phone[100][16])
{
    int position; //the place where the person is in the array
    position = searchName(count, amount, lastNames);
    if (position != -1)
    {
    }

}

I think you should really avoid using char arrays and char pointers. 我认为你应该真的避免使用char数组和char指针。 Use std::vector and std::string instead, they will save you a lot of trouble. 使用std::vectorstd::string代替,它们将为您节省很多麻烦。 std::vector<std::string> is probably all you need. std::vector<std::string>可能就是你所需要的。 Or even better, you should bundle firstName, lastName, email and phone together in a structure. 或者甚至更好,你应该将firstName,lastName,email和phone捆绑在一起。

struct Person
{
    std::string firstName;
    std::string lastName;
    std::string email;
    std::string phone;
}

void deletePerson(std::vector<Person>& persons)
{
    int position = searchName(lastNames); // you could replace this with std::find
    if (position != -1)
    {
    }
}

Try this: 尝试这个:

It uses the template argument deduction and references to simplify things. 它使用模板参数推导和引用来简化事物。 It works in case you change the dimensions of your arrays for whatever reason without changing the function (and possibly the logic). 它适用于您在不更改函数(可能还有逻辑)的情况下因任何原因更改数组的尺寸。

template<class T, int N1, int N2, int N3, int N4, int N5, int N6, int N7, int N8>
void deletePerson(int &, int, T(&r1)[N1][N2], T(&r2)[N3][N4], T(&r3)[N5][N6], T(&r4)[N7][N8]){} 

int main() 
{ 
   char firstNames[100][25], lastNames[100][25], email[100][50], phone[100][16]; 
   int count = 100;
   deletePerson(count, 20, firstNames, lastNames, email, phone); 
} 

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

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