1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "clog.h"
char* repeat_char(char char_to_repeat, int num_repetitions) {
char* s = malloc((num_repetitions + 1) * sizeof(*s));
for(int i = 0; i < num_repetitions; i++) {
s[i] = char_to_repeat;
}
s[num_repetitions] = '\0'; // Last element in the array stored in buffer s.
// │>>>>>> memory shows state of memory as of HERE <<<<<<<<│
return s;
}
int main(int argc, char* argv[]) {
char ch = 'j';
int width = 5;
char* divider = repeat_char(ch, width); // will be on the heap (⚠ calls malloc)
log_str(divider);
free(divider);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2021 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.