0

So I have been searching through stack overflow for a little over an hour and I don't understand why this function is giving me a segmentation error. I want to create a string array, scan strings in through scanf, dynamically change the size of each string and return the string array. Can anyone help? Thank you.

char** readScores(int* count) {
    int c = 0;
    char** arr =(char**)malloc(100 * sizeof(char*));
    char* in;
    while(scanf("%s", in) != EOF) {
        arr[c] = (char*)malloc(strlen(in)+1);
        strcpy(arr[c], in);
    }
    *count = c;
    return arr;
}
10
  • 1
    Did you read your book? Consult and study the documentation for the features you've used? Step through the program with your debugger? Just searching SO for an hour isn't going to cut it as far as debugging goes. Commented Oct 5, 2020 at 22:53
  • 2
    You have to allocate memory for in before scanf Commented Oct 5, 2020 at 22:53
  • I would do this but my professor didn't provide us a book and I am using emacs on a linux system without a debugger. Commented Oct 5, 2020 at 22:54
  • 1
    Then you need to get a book, and you need to install a debugger. Commented Oct 5, 2020 at 22:54
  • @fas thank you I was going crazy! Commented Oct 5, 2020 at 22:55

2 Answers 2

1
char* in;
while(scanf("%s", in) != EOF) {

This tells the computer to read from standard input into the char buffer that in points to.

Which does not exist, because in is not initialised to anything (let alone a valid buffer).

Sign up to request clarification or add additional context in comments.

Comments

0

I would not use scanf only fgets.

You need to allocate memory dor the arr and for every line referenced by elements of arr

char** readScores(size_t *count) {
    size_t lines = 0;
    char** arr = NULL, **tmp;
    char* in = malloc(MAXLINE), *result;
    size_t len;

    if(in)
    {
        do{
            result = fgets(in, MAXLINE, stdin);           
            if(result)
            {
                len = strlen(in);
                tmp = realloc(arr, sizeof(*tmp) * (lines + 1));
                if(tmp)
                {
                    arr = tmp;
                    len = strlen(in);
                    arr[lines] = malloc(len + (len == 0));
                    if(arr[lines])
                    {
                        if(len) memcpy(arr[lines], in, len - 1);
                        arr[lines++][len] = 0;
                    }
                    else
                    {
                        // error handling     
                    }
                }
                else
                {
                    // error handling 
                }
            }
        }while(result);
        free(in);
    }
    *count = lines;
    return arr;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.