使用 STRCPY 将二维字符数组复制到另一个二维数组中

Copy 2d Character Array inAanother 2d Array Using STRCPY

我正在尝试使用字符串函数 strcpy 将一个二维字符数组复制到另一个二维字符数组中,但它给出了访问冲突错误。我不知道我做错了什么。我正在发布代码和错误有人可以告诉我我做错了什么吗

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();
}

Access violation writing location 0x001fbe5c.

countsize的值为60

还有一件事 strcpy 在这一行上起作用 strcpy(tempname , name[j]) 但是当它遇到这一行时 strcpy(name[j] , name[j-1]) 它会抛出访问冲突错误

这个函数声明

void sorting(char *name[],char *meaning[],int size,int count);

不处理 two-dimensional 数组。

例如,如果您有 two-dimensional 个像这样的数组

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 * 的不完整 one-dimensional 指针数组类型,已调整为类型 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" );

您不需要二维字符数组,只需字符串数组。所以你可以不使用 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 的指针可以解释为指向 char 数组(指向字符串)的第一个元素的指针。所以如果你想交换数组中的两个字符串,你只需要交换指向那个字符串的指针。