我正在尝试使用字符串函数strcpy将2d字符数组复制到另一个2d字符数组中,但是它给了我访问冲突的错误。我不知道我做错了什么。我发布代码和错误可以有人告诉我,我做错了什么
int searching(char *name[],char * namesearched,int size)
{
int count =0;
int start = 0;
int end = count;
for(;start<=end;)
{
int mid = (start + end)/2;
if(strcmp(namesearched,name[mid])==0)
{
return mid;
}
else if(strcmp(namesearched,name[mid])==1)
{
end=mid -1;
}
else if(strcmp(namesearched,name[mid])==-1)
{
start = mid +1;
}
}
return -1;
}
void sorting(char **name,char ** meaning,int count)
{
for (int i=0;i<count;i++)
{
for(int j=i+1; j<count; j++)
{
char tempname[100];
char tempmeaning[100];
if(strcmp(name[j-1],name[j])>0)
{
strcpy(tempname,name[j]);
//strcpy(name[j],tempname);
strcpy(name[j-1],name[j]);
strcpy(name[j],name[j-1]);
strcpy(name[j-1],tempname);
strcpy(tempmeaning,meaning[j]);
strcpy(meaning[j],meaning[j-1]);
strcpy(meaning[j-1], tempmeaning);
}
}
}
}
void main()
{
int size=60;
int count=0;
char namesearched[100];
cout << "Enter the name to be searched: ";
cin.getline(namesearched , 100);
char** name= new char * [size];
char** meaning = new char * [size];
for(int i=0;i < size ; i++)
{
name[i]= new char [100];
meaning[i]= new char[100];
count ++;
}
name[0] = "Journalist";
name[1] = "Blister";
name[2] = "List";
name[3] = "Listen";
name[4] = "Novelist";
name[5] = "Song";
name[6] = "Eat";
name[7] = "West";
name[8] = "Idealist";
name[9] = "Industry";
name[10] = "Legalist";
name[11] = "Write";
name[12] = "Medal";
name[13] = "Nation";
name[14] = "Accident";
name[15] = "Nest";
name[16] = "Bird";
name[17] = "Animal";
name[18] = "Lion";//wrong
name[19] = "Pigeon";
name[20] = "Real";
name[21] = "Accept";
name[22] = "Ability";
name[23] = "Bald";
name[24] = "Backbite";
name[25] = "Wakeful";
name[26] = "Absolute";
name[27] = "Wail";
name[28] = "Abiding";
name[29] = "Unacceptable";
name[30] = "Tacker";
name[31] = "Vain";//wrong
name[32] = "Abolish";
name[33] = "Taking";
name[34] = "Unarmed";
name[35] = "Habit";
name[36] = "Notus";
name[37] = "Impecle";
name[38] = "Accelerate";
name[39] = "Agony";
name[40] = "Sulk";
name[41] = "Nowise";
name[42] = "Hypocrisy";
name[43] = "Nape";
name[44] = "Eccentric";
name[45] = "Naturally";
name[46] = "Gratitude";
name[47] = "Mesmerizing";
name[48] = "Epic";
name[49] = "Abstain";
name[50] = "Enactment";
name[51] = "Hammock";
name[52] = "Nodal";
name[53] = "Laborious";
name[54] = "Nonverbal";
name[55] = "Haggle";
name[56] = "Notorious";
name[57] = "Lagger";
name[58] = "Pathetic";
name[59] = "Norms";
meaning[0] = "Sahaafi";
meaning[1] = "Chaala";
meaning[2] = "Fehrist";
meaning[3] = "Sunna";
meaning[4] = "Naval Nigaar";
meaning[5] = "Ganna";
meaning[6] = "Khanna";
meaning[7] = "Maghrib";
meaning[8] = "Tadawuri";
meaning[9] = "Sannat";
meaning[10] = "Zabta Parast";
meaning[11] = "Likhna";
meaning[12] = "Tangha";
meaning[13] = "Qoom";
meaning[14] = "Hadsa";
meaning[15] = "Ghonsla";
meaning[16] = "Parinda";
meaning[17] = "Janwar";
meaning[18] = "Shair";
meaning[19] = "Kabootar";
meaning[20] = "Haqeekat";
meaning[21] = "Qabool";
meaning[22] = "Kabliyat";
meaning[23] = "Ganja";
meaning[24] = "Ghebat Karna";
meaning[25] = "Jagta";
meaning[26] = "Bikul";
meaning[27] = "Gham Karna";
meaning[28] = "Mustakil";
meaning[29] = "NaGawar";
meaning[30] = "Jorna Wala";
meaning[31] = "Gari";
meaning[32] = "Rad kar dena";
meaning[33] = "Dil-chasp";
meaning[34] = "Nehatta";
meaning[35] = "Addat";
meaning[36] = "Dakni hawwa";
meaning[37] = "Rokna";
meaning[38] = "Taiz karna";
meaning[39] = "Sakht Takleef";
meaning[40] = "Roth Jana";
meaning[41] = "Hargiz Nahi";
meaning[42] = "Naffaq";
meaning[43] = "Mankaa";
meaning[44] = "Sanki";
meaning[45] = "Fitratan";
meaning[46] = "Tashakur";
meaning[47] = "Mashoor Karna";
meaning[48] = "Razmiya";
meaning[49] = "Baaz Rakhna";
meaning[50] = "Nifaaz";
meaning[51] = "Jholay ki tarhan ka Bichona";
meaning[52] = "Gutheela";
meaning[53] = "Mehnat Talab";
meaning[54] = "Ghair Lafzey";
meaning[55] = "Takrar Karna";
meaning[56] = "Badnam";
meaning[57] = "Ahista Chalnay walla";
meaning[58] = "Intehai afsoos naak baat";
meaning[59] = "Mayar";
int mid;
sorting( name , meaning , count);
int mid = searching(name,namesearched,count);
if( mid == -1 )
{
char ** tempname = new char* [60];
char ** tempmeaning = new char*[60];
if(count == size)
{
int increase =(10 * size)/100;
size = increase + size;
for(int i=0 ; i<size ; i++)
{
tempname[i] = new char [100];
tempmeaning[i]= new char [100];
}
for(int i = 0; i<count ; i++)
{
strcpy(tempname[i],name[i]);
strcpy(tempmeaning[i],meaning[i]);
}
}
strcpy(tempname[count] , namesearched);
cin >> tempmeaning[count];
count ++;
sorting( tempname , tempmeaning , count);
for (int i =0;i < count ;i++)
{
delete [] name[i];
delete [] meaning[i];
}
//delete [] name;
//delete [] meaning;
name = tempmeaning;
meaning = tempmeaning;
tempmeaning = NULL ;
tempname = NULL;
}
else
{
cout <<"The meaning of " << namesearched << " is: " << meaning[mid] << endl;
}
_getch();
}
访问冲突写入位置0x001fbe5c。
计数和尺寸的值为60
更多strcpy在这一行strcpy(tempname , name[j])
上有效,但当遇到此行strcpy(name[j] , name[j-1])
时,它会抛出访问冲突的错误
答案 0 :(得分:2)
此功能声明
void sorting(char *name[],char *meaning[],int size,int count);
不涉及二维数组。
例如,如果你有像这样的二维数组
char name[60][100];
char meaning[60][100];
然后函数声明看起来像
void sorting( char name[60][100], char meaning[60][100], size_t count );
或
void sorting( char name[][100], char meaning[][100], size_t count );
或
void sorting( char ( *name )[100], char ( *meaning )[100], size_t count );
并且第三个参数的参数值应该等于60。
至于你的函数声明,那么例如这个参数char *name[]
具有类型为char *
的不完整的onnsionalioan指针数组的类型,其被调整为类型char **
。如果相应的参数是一个指向字符串文字的指针数组,那么该函数具有未定义的行为,因为您可能不会更改字符串文字。
所以看起来你正在错误地处理数组,因为它们的定义与函数代码的逻辑不对应。
函数中也没有使用参数size
。
因此,您的代码最初只是错误。
考虑此if语句中的条件
if(strcmp(name[j-1],name[j]))
应该看起来像
if ( strcmp( name[j-1], name[j] ) > 0 )
如果要按升序排序数组,或者像
那样if ( strcmp( name[j-1], name[j] ) < 0 )
如果要按降序对数组进行排序。
编辑:在您添加问题之后,可以看到1)存在内存泄漏,因为最初指向已分配内存的指针会重新分配字符串文字的地址,并且2)您是试图改变不可变的字符串文字。
而不是例如
name[0] = "Journalist";
你必须写
strcpy( name[0], "Journalist" );
答案 1 :(得分:1)
您不需要2d char数组,只需要字符串数组。所以你可以不使用strcpy来做到这一点。类似的东西:
void sorting(char *name[],char *meaning[], int count)
{
for (int i = 0; i < count; i++)
{
for(int j = 1; j < count - i; j++)
{
char *tempname;
char *tempmeaning;
if(strcmp(name[j-1],name[j]) > 0)
{
tempname = name[j];
name[j] = name[j-1];
name[j-1] = tempname;
tempmeaning = meaning[j];
meaning[j] = meaning[j-1];
meaning[j-1] = tempmeaning;
}
}
}
}
char *name[]
是一个指向char的指针数组。指向char的指针可以解释为指向chars数组的第一个元素(指向字符串)的指针。所以如果你想在数组中交换两个字符串,你只需要将指针交换到那个字符串。