Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use calloc in matrix allocation. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int main(int argc, char* argv[]){

char *input_matrix(int width, int height)
{
char *matrix = malloc(sizeof(char) * (width * height + 1)); //+1 for null
char *matrix = calloc(width * height + 1, sizeof(char)); // +1 for null
char *line = NULL;
size_t size;

Expand All @@ -56,15 +56,14 @@ char *input_matrix(int width, int height)

while (strlen(matrix) < width * height)
{
int n = getline(&line, &size, stdin) - 1; // do not count line feed
int n = getline(&line, &size, stdin) - 1; // to not count line feed
if (n > width)
{
printf("\033[1A\033[K"); // go up and erase line
continue;
}
/* Fill the rest with spaces */
memset(line + n, ' ', width - n);
line[width] = '\0'; // terminate modified string
/* Fill the rest with null */
memset(line + n, 0, width - n);
printf("\033[1A%s|\n", line); // show priview of line
strncat(matrix, line, width);
}
Expand Down