简体   繁体   中英

using strcmp with an array

Here is the full code. it keeps freezing now

 typedef struct {
     char *name;
} NAME;

setting the array to be null and then later on i expand it depending on how many entries i need to add.

NAME    *array = NULL;
int itemCount = 0; // items inserted
int arraySize = 0; // size of array
int arrayCount; //gets size of file.
int found = 0;

int Add(NAME item)
{
if(itemCount == arraySize) {

    if (arraySize == 0)
        arraySize = 3;
    else
        arraySize *= 2; // double size of array

    //relocate memory
    void *tempMemory = realloc(array, (arraySize * sizeof(NAME)));

    //error relocating memory
    if (!tempMemory)
    {
        printf("Couldn't relocate the memory \n");
        return(-1);
    }

    array = (NAME*)tempMemory;
}

array[itemCount] = item;
itemCount++;

return itemCount;
}
   void printStruct()
{
    int i;
    for(i = 0; i < arrayCount; i++)
    {
        printf("%s \n", array[i].name);
    }
}

int readFromFile()
{
    int checkResult(char[]);
    FILE *fp;
    fp = fopen("names.txt", "r");

    char names[arrayCount];

    if (fp == NULL)
    {
        printf("Cannot access file \n");
    }else{
        while(!feof(fp))
        {
            fscanf(fp, "%s", names);
            arrayCount++;
            checkResult(names);
        }
    }

    fclose(fp);

    return 1;
    }


int checkResult(char names[]){
    NAME tempStruct;

    int i;
    if(array == NULL)
    {
        tempStruct.name = malloc((strlen(names) + 1) * sizeof(char));
        strcpy(tempStruct.name, names);
        tempStruct.count = 1;
    }
    else
    {
        for(i = 0; i < arrayCount; i++)
        {
            if(strncmp(array[i].name, names, arrayCount)==0)
            {
                printf("MATCH %s", names);
                break;
            }
        }

        if(i == arrayCount)
        {
            tempStruct.name = malloc((strlen(names) + 1) * sizeof(char));
            strcpy(tempStruct.name, names);
        }


        if (Add(tempStruct) == -1)
        {
                return 1;
        }
    }
}

In the main i am freeing the memory and calling the other functions

int main(){
    void printStruct();
    int readFromFile();
    readFromFile();

    printStruct();

    int i;
    for (i = 0; i < arrayCount; i++)
    {
        free(array[i].name);
    }

    free(array);

    return 0;
}

What we're doing here is looping through looking for the first match and breaking the loop when we find it. If we search without finding it anywhere in the array, then we add it...

NAME** array = calloc( MAX_NAMES, sizeof( NAME* ) );
int    count = 0;

int checkName(char names[])
{
    if(!count)
    {
        array[0] = calloc( 1, sizeof( NAME ) );
        array[0]->name = malloc((strlen(names) + 1) * sizeof(char));
        strcpy(array[0]->name, names);
        count = 1;
    }
    else
    {
        int i;

        for(i = 0; i < count; i++)
        {
            if(strcmp(array[i]->name, names)==0)
            {
                printf("MATCH %s", names);
                break;
            }
        }

        if( i == count && count < MAX_NAMES )
        {
            array[count] = calloc( 1, sizeof( NAME ) );
            array[count]->name = malloc((strlen(names) + 1) * sizeof(char));
            strcpy(array[count]->name, names);
            count++;
        }
    }
}

The above code first tests to see that the array is not empty... if it is, then the first element is created for the array and the name is assigned.

Otherwise, it parses the array to see if any entry in the array already matches the name... if it does, it will break the loop and the i == count test will fail, so the name is not added.

If the loop finishes without matching the name, then the i == count test will return true and we add the new name to the end of the array, if there is room in the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM