Ele975
Ele975

Reputation: 372

Print array with pointer in C

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;
}

Upvotes: 0

Views: 123

Answers (3)

Halfa
Halfa

Reputation: 110

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.

Upvotes: 0

chqrlie
chqrlie

Reputation: 144770

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;
}

Upvotes: 1

MikeCAT
MikeCAT

Reputation: 75062

  • 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.

Upvotes: 2

Related Questions