读书人

再发二维数组存储数据有关问题

发布时间: 2012-03-23 12:06:21 作者: rapoo

再发二维数组存储数据问题!
大家好,我遇到这样一个问题,麻烦大家帮帮忙,解答一下,万分感激!

我想将数组StrPart1[2]和StrPart2[2]中字符串内有用信息读出来,存在二维数组Save_Inf[2][2],但是不知道为什么,总是得不到我想要的结果,用cout对Save_Inf[2][2]进行for循环显示,两条记录都和第二条(最后一条)相同,第一条信息丢失了,这是怎么回事?麻烦给解答一下,非常谢谢!


头文件.h中的程序:
.....................................................................
char* Tran_Location(char* );
char* Tran_Load_Place(char* );

.....................................................................
主文件.cpp中的程序:
......................................................................
// Main function for reading the information in EDI file//
// Time: 2007-10-14//Author:QinTao//

#include <iostream>
#include <string.h>
#include "Head.h"
using namespace std;

char* StrPart1[2]={"LOC+147+0320586::5","LOC+147+0070814::5"}; //需要处理的字符串

char* StrPart2[2]={"LOC+6+USTPA","LOC+6+CNSNH"}; //需要处理的字符串

char* Save_Inf[2][2]; //用于储存数组StrPart1和StrPart2中的有用信息



void main()
{
for (int i=0; i<2; i++)
{
Save_Inf[i][0]=Tran_Location(StrPart1[i]);// Find out the infromation about location of container

cout << "Save_Inf["<< i << "][0] = " << Save_Inf[i][0]<< endl <<endl;

Save_Inf[i][1]=Tran_Load_Place(StrPart2[i]); // Find out the infromation about loading place ofthe container

cout <<"Save_Inf["<< i << "][1] = " << Save_Inf[i][1]<< endl <<endl;

}
cout <<"..........................................."<<endl;

for (int j=0;j<2;j++)
{
for (int k=0; k<2;k++)
cout << "Save_Inf["<< j << "][" << k <<"] = " << Save_Inf[j][k]<<endl;
cout << endl;
}
}

.................................................................................................
调用函数.cpp中的程序:
................................................................................................
#include "Head.h"
#include <iostream>
#include <string.h>
using namespace std;

char Location[8];
char Load_Place[20];

//...............................................................................................
// Find out the infromation of location of container
char* Tran_Location(char* Part_Inf) // Notice: "Part_Inf" variable is evaluated by StrPart1[]
{
char temp1[30]= "\'";
strcat(temp1, Part_Inf);

for (int i=0 ;i<strlen(temp1)-3;i++) //e.g. 仅仅读取字符串" LOC+147+0320586::5 "中的0320586这几个数字
{
if (i>8)
{
Location[i-9]=temp1[i];
}
}
Location[7]='\0';

cout << endl <<"The Location of Container is :" <<Location <<"\n"<<endl;

return(Location);
}

//...............................................................................................
// Find out the infromation about the Loading Place of container
char* Tran_Load_Place(char* Part_Inf)
{
char temp1[30]= "\'";
int count=0;

strcat(temp1,Part_Inf);

for (int i=0;i<strlen(temp1);i++) //e.g. 仅仅读取字符串" LOC+6+USTPA "中的USTPA这几个数字
{
if (i>6)
{
Load_Place[i-7]=temp1[i];
count++;
}
}

Load_Place[strlen(temp1)-7]='\0';

cout << "The Loading Place of Container is :" <<Load_Place <<"\n"<<endl;



return(Load_Place);
}

........................................................................
输出结果如下:
.........................................................................

The Location of Container is :0320586

Save_Inf[0][0] = 0320586
The Loading Place of Container is :USTPA

Save_Inf[0][1] = USTPA

The Location of Container is :0070814

Save_Inf[1][0] = 0070814
The Loading Place of Container is :CNSNH

Save_Inf[1][1] = CNSNH
...........................................
Save_Inf[0][0] = 0070814 // 为什么Save_Inf[0][0]= 0070814 而不等于上面的Save_Inf[0][0] = 0320586 ??
Save_Inf[0][1] = CNSNH // 而是和Save_Inf[1][0] = 0070814相同呢???

Save_Inf[1][0] = 0070814
Save_Inf[1][1] = CNSNH

Press any key to continue

麻烦各位给解答一下!!感激


[解决办法]
char* Tran_Location(char* );
char* Tran_Load_Place(char* );
分别使用char Location[8];char Load_Place[20];作为返回值

char* Save_Inf[2][2];是字符串子针,*Save_Inf[0][0],*Save_Inf[1][0]都指向Location这个地址;
所以当你再次更改Location,赋给Save_Inf[1][0]时也就同时更改了Save_Inf[0][0]的值

解决: 根据Locatio数组的大小动态分配Save_Inf[1][0]空间,然后strcpy
或者使用Locatio[2]
或者
这个程序这种赋值思路本身不是一种好的方法

读书人网 >C++

热点推荐