0

I am creating JSON files using the parser frozen.h library. Everything works fine except when i try to parse arrays.

This is my code:

int installation_info_graphs(int roll_array[], int roll_len, int pitch_array[], int pitch_len, int tn_array[], int tn_len, int snr_array[], int snr_len)
{

    char *roll_index = create_index_string_list(roll_array, roll_len);
    char *pitch_index = create_index_string_list(pitch_array, pitch_len);
    char *tn_index = create_index_string_list(tn_array, tn_len);
    char *snr_index = create_index_string_list(snr_array, snr_len);
    return json_fprintf("/api/installation/inst_info_graphs.json", "{roll: %M,roll_labels: %M,"
                        "pitch: %M, pitch_labels: %M, true_north: %M, true_north_labels: %M,"
                        "snr: %M, snr_labels: %M}", roll_array, roll_len, roll_index, pitch_array, pitch_len, pitch_index, tn_array,
                        tn_len, tn_index, snr_array, snr_len, snr_index);
}
//where the function to create an index string array is
char *create_index_string_list(char *lst, int len) {
    char *index_string_list = (char *) malloc(len * sizeof(char *));
    char index_str[3];
    for (int i = 0; i < len; i++) {
        sprintf(index_str, "%02d", i);
        char *str = (char *) malloc((strlen(index_str) + 3) * sizeof(char));
        strcpy(str, "1:0");
        strcat(str, index_str);
        index_string_list[i] = str;
    }
    return index_string_list;
}

What i get after trying to execute the code is a segmentation fault. I think i am using an incorrect type formatter, and that this library have the appropiate one, but I cannot find it on the documentation. Could someone help me with this issue. Thanks!

1 Answers1

0

Your compiler is probably giving you a warning like this, which you are ignoring:

warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast

This is because you are defining char *index_string_list (an array of characters), when you really want an array of character pointers. Your function is meant to return an array of strings.

So you'll need to change the return type of the function, and also the type of index_string_list to be char **.

Fixed code:

char **create_index_string_list(char *lst, int len) {
    char **index_string_list = malloc(len * sizeof(char *));
    char index_str[3];
    for (int i = 0; i < len; i++) {
        sprintf(index_str, "%02d", i);
        char *str = malloc((strlen(index_str) + 3) * sizeof(char));
        strcpy(str, "1:0");
        strcat(str, index_str);
        index_string_list[i] = str;
    }
    return index_string_list;
}

I've also removed the unnecessary casts on the return value from malloc(), as this is considered bad practice.

You'll also need to change all the variables that store the return value from this function (e.g. char *roll_index) to be char **. I don't know if json_fprintf() can print arrays of strings though.

I also worry that the lst parameter to create_index_string_list() isn't actually used - this probably indicates some kind of bug.

pmacfarlane
  • 3,057
  • 1
  • 7
  • 24