-3

As a part of my homework with C, I had to make a function which splits a string to all the words starting with the key(which is a letter) inserted. Everything works great except for the free function, When I try to free the dynamic matrix by function (rows and then skeleton) I get an error that the program has triggered a breakpoint.

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

char **Split(char *str, char letter, int *size);
void free_mat(char **mat, int size);

int main() {
    int size, i;
    char letter;
    char STR[100];
    char **strings_arr;
    printf("Please enter a string:\n");
    flushall;
    gets(STR);
    printf("Please enter a letter for a check:\n");
    letter = getchar();
    strings_arr = Split(STR, letter, &size);

    if (size > 0) {
        printf("The number of words that starts with the letter '%c' in the string '%s' is: %d\n\n", letter, STR, size);
    }
    printf("The words are:\n");
    for (i = 0; i < size; i++) {
        printf("%d . %s\n", i+1,strings_arr[i]);
    }
    free_mat(strings_arr, size);
    return 0;
}

void free_mat(char **mat, int size)  
{
    int i;
    for (i = 0; i < size; i++)
    {
        free(mat[i]);
    }
    free(mat);
}
char **Split(char *str, char letter, int *size) {
    int rows = 0, i, lengh = 0, j = 0, n = 0, m;
    char **strings_array;

    if ((str[0] == letter) || (str[0] == letter + 32) || str[0] == letter - 32) {
        rows++; 
    }
    for (i = 0; str[i] != '\0'; i++) {
         if (str[i] == ' ') {   
            if ((str[i + 1] == letter) || (str[i + 1] == letter + 32) || str[i + 1] == letter - 32) {
                rows++; 
            }
        }
    }
    if (rows == 0) {    
        printf("There are no words starting with '%c' letter in this string\n\n", letter);
    }
    i = 0;
    strings_array = (char*)malloc(rows * sizeof(char)); 
    if ((str[0] == letter) || (str[0] == letter + 32) || str[0] == letter - 32) { 
        while (str[i] != ' ' && str[i] != '\0') {
            lengh++;
            i++;
        }
        strings_array[j] = (char*)malloc((lengh + 1) * sizeof(char)); 
        for (n = 0; n < lengh; n++) {
            strings_array[j][n] = str[n];
        }
        strings_array[j][n] = '\0';
        j++;    
    }
    for (i = 1; str[i] != '\0'; i++) {
            if (letter == str[i] || letter == str[i] - 32 || letter == str[i] + 32) {
                lengh = 0;
                //k = 0;
                m = i;  
                while (str[m] != ' ' && str[m] != '\0') {
                    lengh++;
                    m++;
                }
                strings_array[j] = (char*)malloc(lengh + 1);    

                for (n = 0; n < lengh; n++) {
                    strings_array[j][n] = str[i++]; 
                }
                strings_array[j][n] = '\0';
                j++;    
            }
    }
    *size = rows;   // sends back the number of words by referance
    return strings_array;
}

Thanks!

R_L
  • 33
  • 1
  • 8

1 Answers1

0

A breakpoint is something that you manually insert in the code from your IDE or equivalent. It is used for debugging. When you run the code it is intended to stop when it reaches a breakpoint. So just remove the breakpoint and it should work as expected.

Note: Remove only the breakpoint. Not the code on that line.

You mentioned in comments below that you're using Visual Studio 2015. Here is the documentation for breakpoints in that software: https://learn.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2019

But there are some other things about your code. First of all, use fgets instead of gets. Second, you seem to have posted the wrong version or something, because free_mat will not compile. However, that was easily solved by changing arr to mat.

There's also another error that xing mentioned in the comments. Change strings_array = (char*)malloc(rows * sizeof(char)) to strings_array = malloc(rows * sizeof(*strings_array)). The cast is not necessary, you picked the wrong type for the argument to sizeof and if you pass a dereferenced pointer instead of the type you'll save yourself a lot of problems in the future.

klutt
  • 30,332
  • 17
  • 55
  • 95
  • I changed that just for the post, its not working with arr too, mybad, edited the post its all with mat now and still not working. – R_L May 26 '19 at 19:23
  • @R_L As I said. A breakpoint is something that you - accidentally or on purpose - have inserted on that row. You need to remove it. Breakpoints have nothing to do with the code. – klutt May 26 '19 at 19:26
  • I dont get it, in the row its getting a breakpoint I only have free(mat), If I remove it then I can't free the skeleton.. – R_L May 26 '19 at 19:28
  • @R_L What program are you using for writing the code? – klutt May 26 '19 at 19:30
  • I use Visual Studio 2015 – R_L May 26 '19 at 19:32
  • https://learn.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2019 – klutt May 26 '19 at 19:33