Home > Enterprise >  Why is the integer in the struct a garbage values even though it was changed?
Why is the integer in the struct a garbage values even though it was changed?

Time:06-29

So, I implemented split in C, now I know strtok exists, but I wanted to implement it, so my function returns a struct that has the string array and the length, which is decided by the number of times the delimiter occurs and whether it's the first value or not, here's the code.

split.h

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int count_delm(char *, char);
char *make_str(char);
struct split_output {
    char **arr;
    int size;
};
typedef struct split_output split_arr;
split_arr *split(char *, char);

split.c

#include "split.h"

int count_delm(char *str, char delm) {
    register int lpvar = 0;
    register int counter = 0;
    while (str[lpvar] != '\0') {
        if (str[lpvar] == delm) {
            counter  ;
        }
        lpvar  ;
    }
    return counter;
}

char *make_str(char ch) {
    char *ret_str = (char *)malloc(2);
    ret_str[0] = ch;
    ret_str[1] = '\0';
    return ret_str;
}

split_arr *split(char *str, char delm) {
    int num_delm = count_delm(str, delm);
    char **final_arr;
    register int lpvar = 0;
    register int arr_counter = 0;
    char *concat_str = (char *)malloc(2);
    int ret_size = 0;
    if (str[0] == delm) {
        concat_str = make_str(str[1]);
        final_arr = (char **)malloc(sizeof(char *) * (num_delm));
        ret_size = num_delm;
        lpvar  ;
    } else {
        concat_str = make_str(str[0]);
        final_arr = (char **)malloc(sizeof(char *) * (num_delm   1));
        ret_size = num_delm   1;
    }
    while (1) {
        if (str[lpvar   1] != '\0') {
            if (str[lpvar   1] != delm) {
                concat_str = strcat(concat_str, make_str(str[lpvar   1]));
                lpvar  ;
            } else {
                final_arr[arr_counter] = concat_str;
                arr_counter  ;
                if (str[lpvar   2] != '\0') {
                    lpvar  ;
                    lpvar  ;
                    concat_str = make_str(str[lpvar]);
                }
            }
        } else {
            arr_counter  ;
            final_arr[arr_counter] = concat_str;
            printf("%s is the last at pos %d\n", concat_str, arr_counter);
            break;
        }
    }
    split_arr *ret_struct = (split_arr *)malloc(sizeof(split_arr));
    (*ret_struct).size = ret_size;
    (*ret_struct).arr = final_arr;
    return ret_struct;
}

That was the code of the split implementation, here's the code that tests it. test.c

#include "split.h"

int main() {
    char *x = "lryabruahsdfads";
    split_arr output = *(split(x, 'a'));
    char **read = output.arr;
    int len = output.size;
    int loop = 0;
    printf("size: %d", len - 1);
    for (loop = 0; loop < len; loop  ) {
        puts(*(read   loop));
    }
    return 0;
}

Here's the output when executed:

ds is the last at pos 4
size: 3lry
bru
hsdf
Segmentation fault (core dumped)

Why is output.size 3lry? I de-referenced the struct pointer to get the struct so there's nothing wrong with that. I can't find the error, I've been trying to debug for almost an hour.

CodePudding user response:

In your split.c file over here:

else{
   arr_counter  ; //comment this line
   final_arr[arr_counter]=concat_str;
   printf("%s is the last at pos %d\n",concat_str,arr_counter);
   break;
  }

You are incrementing variable arr_counter which you should not because you already incremented it inside the if. Just comment out this line and your code works fine. And your output is fine just add a line return to the printf statements like:

printf("size: %d \n",len-1);
  • Related