使用指针将两个char数组连接为单个char数组 [英] concatenate two char arrays into single char array using pointers

查看:129
本文介绍了使用指针将两个char数组连接为单个char数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一个具有名称(例如john doe)的文本文件,并查找名字和姓氏.然后,我要使用这两个char数组,并使用指针将它们连接在一起. 已注释掉的代码是有效的代码,它将两个char数组放入一个char数组中,即将它们串联在一起. 该项目要求我使用指针,并且我使用char数组.我不是要你为我做这件事,但是请帮助我意识到我在做错什么.谢谢

I am trying to take a text file with names (ex: john doe) and fin the first name and last name. Then, I want to take these two char arrays and concatenate them together using pointers. The code that is commented out is working code that takes the two char arrays and puts them into a single char array ie concatenating them together. This project requires that I use pointers, and that I use char arrays I am not asking for you to do it for me, but please help me realize what I am doing wrong. Thanks

我得到的错误是段错误..so我正在稀疏我的playerPtr超出了某个范围?

the error I am getting is a seg fault..so Im thining my playerPtr is going out of bounds somewhere??

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 != '\0')
    //while(tempfName[length] != '\0')
{

      tempfName++;
}
      strcopy(tempName,tempfName);

  //now add space after first name
     *tempName = ' ';
      tempName++;
      //tempfName[length] = ' ';
      //tempfName++;
      //length++;

      while(*templName != '\0')
    //while(templName[index] != '\0')
  {
        templName++;
    //tempfName[length] = templName[index];
    //length++;
    //index++;
  }
      strcopy(tempName,templName);
      //tempName++;
      //tempfName[length]='\0';          
      strcopy((*playerPtr).name,tempName);
      playerPtr++;
  fin >> tempfName;
  fin >> templName;
      }
   }
}
delete[] tempfName;
delete[] templName;
delete[]tempName;
}

推荐答案

我看到的问题(也在注释中提到):

Problems that I see (mentioned in comments too):

循环递增tempFNametempLName

Incrementing tempFName and tempLName in loops

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

在上述循环的结尾,tempFName指向字符串的末尾-指向终止的空字符. strcopy不应将任何内容复制到tempName.

At the end of the above loop, tempFName points to the end of the string - it points to the terminating null character. strcopy should copy nothing to tempName.

循环有相同的问题:

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

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

Setting the value of *tempName after the first loop

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

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

This will be valid only if tempName points to the end of the copied string after the call to strcopy. If not, you are just setting the value of the first character in tempName to ' '. Incrementing tempName makes sense only of tempName points to the end of the copied string. Otherwise, it points to the second character.

由于上述错误,在for循环的第一次迭代之后,您的代码将因错误的内存访问限制而出错.此后没有任何事情可以依靠以合理的方式行事.

As a result of the above errors, your code is subject to errors caused by out of bound memory access after the first iteration of the for loop. Nothing after that can be relied upon to behave in a reasonable manner.

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

I suggest the following changes to fix the above errors.

根本不增加变量tempFNametempLName

Don't increment the variables tempFName and tempLName at all

您根本不需要.

删除行:

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

只需使用:

        strcopy(tempName,tempfName);

使用临时指针转到tempName

Use a temporary pointer to go to the end of tempName

第一次拨打strcopy后,请使用:

After the first call to strcopy, use:

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

将临时指针用于第二个strcopy

Use the temporary pointer for the second strcopy

删除行:

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

替换行:

        strcopy(tempName,templName);

使用

        strcopy(temp,tempfName);

替代策略

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

If you implement your version of strcat, you can simply use:

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

这将消除for循环中的许多混乱情况.

That will remove much of the clutter in the for loop.

这篇关于使用指针将两个char数组连接为单个char数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆