N
N
newmersedez2020-11-16 13:41:11
C++ / C#
newmersedez, 2020-11-16 13:41:11

How to properly sort a struct by dynamic string field using qsort?

I am writing a command interpreter. There is an input file that contains instructions of the form:

myvar=15;
bg=25;
ccc=bg+myvar;
print ccc;
bg=ccc*myvar;
var=12;
b=c+d;
az=128;
print;


Faced such problem: it is impossible to sort the structure by the name field. I searched the forums for implementation methods, and it would seem that everything should work, but in the end, sorting does not work at all, as if it does not start. The structure itself looks like this:
typedef struct memoryCell
{
    char    *name;
    int     data;
}memoryCell;


The code snippet itself, in which sorting is applied, looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "memorycell.h"

char    **parse_string(char string[], int *size);          // просто возвращает массив строк вида "myvar" "=" "15" ";"
int     struct_cmp_by_name(const void *a, const void *b);
int     dichotomy_search(memoryCell *variables, int left, int right, char *string);

/*task - variables processing
returns 1 if correct*/
int     variable_processing(FILE *file, memoryCell *variables)
{
    int     i, j;
    char    **parsed_string = NULL;
    char    string[BUFSIZ];
    int     size;
    int     amount_of_variables;
    int     counter;

    size = 0;
    counter = 1;
    amount_of_variables = 0;
    variables = NULL;
    while(!feof(file))
    {
        fgets(string, sizeof(string), file);
        strtok(string, "\n");
        parsed_string = parse_string(string, &size);

        /*initialization of variable*/
        if(strcmp(parsed_string[0], "print") && size == 4)
        {
            variables = (memoryCell *)realloc(variables, ++amount_of_variables * sizeof(memoryCell));
            variables[amount_of_variables - 1].name = (char *)malloc(strlen(parsed_string[0]) * sizeof(char));
            qsort(variables, amount_of_variables - 1, sizeof(memoryCell), struct_cmp_by_name);
            strcpy(variables[amount_of_variables - 1].name, parsed_string[0]);
            variables[amount_of_variables - 1].data = atoi(parsed_string[2]);
            printf("[INITIALIZATION]: %s == %d\n\n", variables[amount_of_variables - 1].name, variables[amount_of_variables - 1].data);
        }

        /*operations with variables*/
        else if(strcmp(parsed_string[0], "print") && size == 6)
        {
            //проверка на существование элементов операции и произведение операции
            printf("[OPERATION]: new operation\n\n");
        }

        /*print variables*/
        else if(!strcmp(parsed_string[0], "print"))
        {
            if(size == 2)
            {
                printf("[PRINT ALL]: %s == %d\n", variables[0].name, variables[0].data);
                for(i = 1; i < amount_of_variables; i++)
                {
                    printf("------------ %s = %d\n", variables[i].name, variables[i].data);
                }
                printf("\n");
            }
            else if(size == 4)
            {
                // if(dichotomy_search(variables, 0, amount_of_variables - 1, parsed_string[2]))
                // {
                //     printf("\n%s is exist!!!\n\n", parsed_string[2]);
                // }
                // else
                //     return (counter);
            }
            else
            {
                return (counter);
            }
        }
        counter++;
    }
    for(i = 0; i < amount_of_variables; i++)
    {
        printf("%s = %d\n", variables[i].name, variables[i].data);
    }
    return (0);
}

int     struct_cmp_by_name(const void *x, const void *y)
{
    memoryCell *a = (memoryCell *)x;
    memoryCell *b = (memoryCell *)y;
    return strcmp(a->name, b->name);
}

/*return 1 - correct(found)
return 0 - incorrect(didn`t find)*/
int dichotomy_search(memoryCell *variables, int left, int right, char *string) 
{
    //todo search element in struct
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-11-16
@newmersedez

qsort(variables, amount_of_variables - 1, sizeof(memoryCell), struct_cmp_by_name);

Well, everything is written correctly if you really want to sort the array without the last element.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question