使用指针将两个字符数组连接成一个字符数组

concatenate two char arrays into single char array using pointers

本文关键字:字符 数组 连接 一个 指针 两个      更新时间:2023-10-16

我正在尝试使用一个名为(例如:john doe)的文本文件,并查找名字和姓氏。然后,我想取这两个char数组,并使用指针将它们连接在一起。被注释掉的代码是工作代码,它将两个字符数组放在一个字符数组中,即将它们连接在一起这个项目要求我使用指针,并使用字符数组我不是要求你为我做这件事,但请帮助我意识到我做错了什么。感谢

编辑:我得到的错误是seg错误。。所以我在想我的球员Ptr在哪里出界了??

void readPlayer(char *finName2, player *playerPtr)
{
player *playerHome = playerPtr;
ifstream fin;
char *tempfName= new char[20];
char *templName= new char[20];
char *tempName= new char[20];
char *tempNameHome = tempName;
fin.open(finName2);
if(!fin.good())
{
cout << "Error with player file!" << endl;       
}
else
{
fin >> tempfName;
fin >> templName;  //prime file
cout << tempfName << templName;
while(fin.good())
{
  for(int i =0;i<5;i++)
  {
    //find the length
    //int index =0, length=0;
while(*tempfName != '')
    //while(tempfName[length] != '')
{
      tempfName++;
}
      strcopy(tempName,tempfName);
  //now add space after first name
     *tempName = ' ';
      tempName++;
      //tempfName[length] = ' ';
      //tempfName++;
      //length++;
      while(*templName != '')
    //while(templName[index] != '')
  {
        templName++;
    //tempfName[length] = templName[index];
    //length++;
    //index++;
  }
      strcopy(tempName,templName);
      //tempName++;
      //tempfName[length]='';          
      strcopy((*playerPtr).name,tempName);
      playerPtr++;
  fin >> tempfName;
  fin >> templName;
      }
   }
}
delete[] tempfName;
delete[] templName;
delete[]tempName;
}

您的tempfName&templName会一直递增,并且它们会移动到分配的内存之外。你需要重置他们的位置
另外,我可以看到

fin >> tempfName;
fin >> templName;

在for循环中,意思是鳍。Good每5次只检查一次。

我看到的问题(也在评论中提到):

增加循环中的tempFNametempLName

        while(*tempfName != '')
        {
           tempfName++;
        }
        strcopy(tempName,tempfName);

在上面循环的末尾,tempFName指向字符串的末尾——它指向终止的null字符。strcopy不应向tempName复制任何内容。

循环也有同样的问题:

        while(*templName != '')
        {
           templName++;
        }
        strcopy(tempName,templName);

在第一次循环后设置*tempName的值

        //now add space after first name
        *tempName = ' ';
        tempName++;

只有当tempName在调用strcopy之后指向复制字符串的末尾时,这才有效。如果不是,则只是将tempName中第一个字符的值设置为' '。递增tempName只有指向复制字符串末尾的tempName才有意义。否则,它将指向第二个字符。

由于上述错误,在for循环的第一次迭代之后,您的代码可能会出现由于内存访问超出限制而导致的错误。在那之后,任何事情都不能指望以合理的方式行事。

我建议进行以下更改以修复上述错误。

根本不增加变量tempFNametempLName

你根本不需要。

删除行:

        while(*tempfName != '')
        {
           tempfName++;
        }

仅使用:

        strcopy(tempName,tempfName);

使用临时指针转到tempName的末尾

第一次调用strcopy后,使用:

        char* temp = tempName;
        while ( *temp != '' ) ++temp;
        *temp = ' ';
        ++temp;
        *temp = '';

将临时指针用于第二个strcopy

删除行:

        while(*templName != '')
        {
           templName++;
        }

更换线路:

        strcopy(tempName,templName);

带有

        strcopy(temp,tempfName);

备用策略

如果您实现strcat版本,您可以简单地使用:

tempName[0] = '';
strcat(tempName, tempFName);
strcat(tempName, " ");
strcat(tempName, tempLName);

这将去除for循环中的大部分杂波。