简体   繁体   中英

Why doesn't my matrix store all my values? (C)

I am working on a problem in which i have to print out the reverse of the user-given words from the cmd. I also have to increase the allocated memory in case the number of inputs are greater than the originally given memory. Memory allocation and reallocation appear to be working fine. For some reason, my matrix only stores the last given input in all of its memory.

What is the problem? I even tried printing out the matrix cell values, but they are all filled with the last input.

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

char** create_matrix(int cap);
char** matrix_reallocation(char** matrix, int* cap);
void free_matrix(char** matrix, int cap);
void print_reverse_matrix(char** matrix, int words);

int main(){


    int cap = 8;
    char** matrix = create_matrix(cap);


    char tmp_word[1025];
    int words = 0;
    while (fscanf(stdin,"%s",tmp_word) != EOF){
        matrix[words] = tmp_word;
        words++;
        printf("Words stored: %d, Capacity: %d\n", words, cap);
        if (words == cap){
            printf("Limit reached, reallocating....\n");
            matrix = matrix_reallocation(matrix, &cap);
        }
    }


    //printf("%s", matrix[0]);
    //printf("%s", matrix[1]);
    //printf("%s", matrix[2]);
    //printf("%s", matrix[3]);

    print_reverse_matrix(matrix,words);




    free_matrix(matrix,cap);
    return 0;
}


char** create_matrix(int cap){
    char** matrix = malloc(sizeof(char*) * cap);
    for (int i = 0; i < cap; ++i)
    {
        matrix[i] = malloc(sizeof(char*) * 1025);
        if (matrix[i] == NULL){
            printf("Memory allocation failed\n");
            exit(1);
        }
    }

    return matrix;
}

char** matrix_reallocation(char** matrix, int *cap){
    char** new_matrix = realloc(matrix, sizeof(char*) * *cap * 2);
    printf("1D reallocated\n");
    *cap *= 2;
    printf("Capacity increased\n");
    for (int i = (*cap/2); i < *cap; ++i)
    {
        new_matrix[i] = malloc(sizeof(char*) * 1025);
        if (new_matrix[i] == NULL){
            printf("Memory allocation failed\n");
            exit(1);
        }
    }

    printf("Succesful reallocation, new capacity: %d\n", *cap);
    return new_matrix;
}

void print_reverse_matrix(char** matrix, int words){
    for (int i = (words - 1); i >= 0 ; i--)
    {
        printf("%d ", i+1);
        for (int j = (strlen(matrix[i]) - 1); j >= 0 ; j--)
        {
                printf("%c ", matrix[i][j]);
        }
        printf("\n");
    }
}

void free_matrix(char** matrix, int cap){
    for (int i = 0; i < cap; ++i)
    {
        free(matrix[i]);
    }
    free(matrix);
}

In each iteration of the loop you overwrite tmp_word and then assign a row in the matrix to it, making them all point to the same string. Instead, you should copy it to the matrix:

char tmp_word[1025];
int words = 0;
while (fscanf(stdin,"%s",tmp_word) != EOF){
    strcpy(matric[words], tmp_word); /* Here! */
    words++;
    printf("Words stored: %d, Capacity: %d\n", words, cap);
    if (words == cap){
        printf("Limit reached, reallocating....\n");
        matrix = matrix_reallocation(matrix, &cap);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM