Search code examples
c++sortingmultidimensional-arraybubble-sortc-strings

Copy 2d Character Array inAanother 2d Array Using STRCPY


I am trying to copy a 2d character array into another 2d character array using the string function strcpy but it's giving me the error of access violation. I don't know what is it that I am doing wrong. I am posting the code and error can somebody tell me what is it that I am doing wrong

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.

The value of count and size is 60

One thing more strcpy works on this line strcpy(tempname , name[j]) but when it encounter this line strcpy(name[j] , name[j-1]) it throws me the error of access violation


Solution

  • This function declaration

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

    does not deal with two-dimensional arrays.

    For example if you have two-dimensional arrays like this

    char name[60][100];
    char meaning[60][100];
    

    then the function declaration will look like

    void sorting( char name[60][100], char meaning[60][100], size_t count );
    

    or

    void sorting( char name[][100], char meaning[][100], size_t count );
    

    or

    void sorting( char ( *name )[100], char ( *meaning )[100], size_t count );
    

    and the value of the argument for the third parameter should be equal to 60.

    As for your function declaration then for example this parameter char *name[] has type of incomplete one-dimensional array of pointers of type char * that is adjusted to type char **. And if the corresponding argument is an array of pointers to string literals then the function has undefined behavior because you may not change string literals.

    So it seems you are processing the arrays incorrectly that is their definitions do not correspond to the logic of the function code.

    Also parameter size is not used in the function.

    Thus your code simply wrong initially.

    Take into account that the condition in this if statement

    if(strcmp(name[j-1],name[j]))
    

    should look either like

    if ( strcmp( name[j-1], name[j] ) > 0 )
    

    if you want to sort the arrays in the ascending order or like

    if ( strcmp( name[j-1], name[j] ) < 0 )
    

    if you want to sort the arrays in the descending order.

    EDIT: After you appended your question then it is seen that 1) there are memory leaks because the pointers that initially pointed to the allocated memory are reassigned with addresses of string literals and 2) you are trying to change string literals that are immutable.

    Instead of for example

    name[0] = "Journalist";
    

    you have to write

    strcpy( name[0], "Journalist" );