0

I have a pointer to an array of char and I need to print the entire array (not one after the other but the entire array). I have the error: zsh: segmentation fault. I don't understand why. My code take a text from stdin and put the chars in an array. If the first array char_array isn't big enough I put them in the second one called bigger_array.

int main() {
    int c;
    int index = 0;
    size_t size = 100;                        
    char *char_array = malloc(size * sizeof(char)); 
    if (char_array != 0) {
        while ((c = getchar()) != EOF && index < size) {
            char_array[index] = c;
            index++;
        }
        size_t newSize = size * 100;
        char *bigger_array = realloc(char_array, newSize * sizeof(char)); 
        if (bigger_array != 0) {
            bigger_array = char_array;
            while ((c = getchar()) != EOF && index < newSize) {
                bigger_array[index] = c;
                index++;
            }
            size = newSize;
        }
    }
    printf(*char_array);
    return 0;
}
3
  • You are using bigger_array even when the first array had enough elements for the input. Commented Sep 26, 2020 at 13:24
  • Trying to print what is pointed at by char_array when char_array == 0 is bad. Commented Sep 26, 2020 at 13:26
  • When printing out a array your best bet is to use a loop, but you need to look over your code, what is it you want to check with char_array != 0, if you reallocate memory to a new char pointer why do you then have bigger_array = char_array?. Also you are not taking a file from stdin Commented Sep 26, 2020 at 13:28

3 Answers 3

2
  • bigger_array = char_array; should be char_array = bigger_array;. Otherwise, the pointer to newly allocated buffer is overwritten to the pointer to old buffer and bad thing will happen.
  • printf(*char_array); is bad because it is passing a integer (one character) where pointer is required. It should be printf("%.*s", index, char_array);. Note that the length to print is specified and you don't need to add terminating null-character thanks to that.
Sign up to request clarification or add additional context in comments.

Comments

1

There are multiple issues in your code:

  • you do not include <stdio.h> nor <stdlib.h>
  • you should free char_array and store big_array to char_array so char_array points to the bigger allocated array.
  • printf(*char_array) has undefined behavior for multiple reasons. You can print the characters with fwrite(char_array, 1, index, stdout) or with printf("%.*s\n", (int)index, char_array).
  • there is no need for separate loops, just reallocate the array on demand.

Here is a modified version:

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

int main() {
    int c;
    size_t index = 0;
    size_t size = 100;
    char *char_array = malloc(size);
    if (char_array != NULL) {
        while ((c = getchar()) != EOF) {
            if (index == size) {
                size_t new_size = size + size / 2 + size / 8; /* increase by the golden ratio */
                char *new_array = realloc(char_array, new_size);
                if (new_array == NULL) {
                    fprintf(stderr, "out of memory\n");
                    free(char_array);
                    return 1;
                }
                size = new_size;
                char_array = new_array;
            }
            char_array[index++] = c;
        }
        printf("%.*s\n", (int)index, char_array);
    }
    return 0;
}

Comments

0

Considere this as a comment ^^

I advise you to use flags for warning with your complier. for exemple for gcc flags -W -Wextra -Wshadow are good to use and prevent lost of time.

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.