0
votes

A cs50 problem set I've been working on sent me down a rabbit hole. I'm new to programming, and I've been working on a program in c to put a get_string result into a 2-d array, with each word separated in the array (which really doesn't apply to the cs50 problem set, so don't worry about that). There's no doubt simpler and better ways to achieve what I am trying to do, but for this experiment I just want to know what I am missing.

As far as I can tell (with my numerous debug printf's), the first part of the function is correctly, creating an array with enough memory to a) store all the words separately, and b) store the largest word in the get_string.

The second part is meant to count along the get_string length, assigning each character into the appropriate index in the newly created array. This is where I have issues. I hit a segmentation fault every time, in different spots depending on whats in the get_string.

I've input a few random strings into the function to test it.

"One by one they counted alphabetical letters". This stops at the space after "they".

"I like icecream" stops after "like",

"Flavor" stops right before "r".

When I use debug50 (in the cs50 IDE), I can get the program to loop a few more times by pausing it before it runs the "isspace" check.

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

void readLevel();


int main(void)
{
   // string text = get_string("Text:\n");
   readLevel();
}

void readLevel()
{
/* you can ignore all this
    int l = 0; //letters per 100 words
    int s = 0; //number of sentences per 100 words
    int indx = 0.0588 * l - 0.296 * s - 15.8;
*/
    string text = get_string("Text:\n");

    string sizeTextCopy = text;
    string sortTextCopy = text;


    //debug prints
    printf("size copy is: %s\n", sizeTextCopy);
    printf("sort copy is: %s\n", sortTextCopy);
    //

    int a = 0;
    int b = 0;
    int bsz = 0;

    for (int c = 0; strlen(sizeTextCopy) > c; c++)
    {
        char sTCChar = sizeTextCopy[c];

        if (isspace(sTCChar))
        {
            //debug prints
             printf("b is : %i\n", b);
             printf("a is : %i\n", a);
             //
            b++;
            a++;
            while (b > bsz)
            {
             bsz = b;
             //debug prints
             printf("bsz is : %i\n", bsz);
             //
            }
            b = 0;
        }
        else
        {
            //debug prints
             printf("b is : %i\n", b);
            //
            b++;
        }
    }

    string firstArr [a][bsz];



    int i = 0;
    int t = 0;
    //debug prints
    int sTCL = strlen(sizeTextCopy);
    printf("strlen is: %i\n", sTCL);

    for (int w = 0; strlen(sizeTextCopy) > w; w++)
    {
        char textsortchar = text[w];
        //debug prints
        printf("tsc is %c\n", textsortchar);
        printf("%i\n", w);

        if (isspace(textsortchar))
        {
            firstArr [i][t] = &textsortchar;
            i++;
            //debug prints
             printf("letter %i of word %i is : %s\n", t, i, firstArr[i][t]);
            //
            t = 0;
        }
        else
        {
            firstArr [i][t] = &textsortchar;
            //debug prints
             printf("letter %i of word %i is : %s\n", t, i, firstArr[i][t]);
            //
            t++;
        }
    }
// this was to print out the array, but ^that^ doesn't work yet.
    for (int s111 = 0; a > s111; s111++)
        for (int s112 = 0; s112 < sizeof(firstArr[s111]); s112++)
            printf("%s", firstArr[s111][s112]);

}

Please don't murder me for my code. I literally started cs50 last week. I'm not really asking for better code, or what is written badly, I would just like to know why it errors. Thanks for taking a look.

1
If you are getting a segfault then you are overwriting memory somewhere - either on the stack or an array index. If you run this in a debugger, you should be able to see where it is happening. Learning how to debug a program is just as important as the language. Does your error show a stack trace or other debugging details?OldProgrammer

1 Answers

0
votes

I believe your issue is that you have declared firstArr as an 2 dimensional array of strings.

Looking at your code, it seems like string is char * (as you pass it to strlen).

This means you have ended up with a 2 dimensional array of pointers to char. Your later code sets all of these pointers to point to the address of a local char variable (textsortchar). When you try to print this using printf, you are accessing the address of a local char variable as if it was a char *, so the print starts reading through stack space it shouldn't, looking for a terminating '\0'.

firstArr should instead be a 2 dimensional array of char as you are storing individual characters in each spot. This will remove the need for the & in the assignment from textsortchar (which is getting its address, not its value) and the subsequent printf can use "%c" in the format, so you are not trying to print a string using the address of a single char