fix: examples

This commit is contained in:
2025-09-05 00:18:08 +02:00
parent 24f6ed0f84
commit 4f71eed36f
11 changed files with 69 additions and 56 deletions

View File

@@ -9,15 +9,17 @@ int main(void) {
char *c_str;
/* Allocate a new dynamic string with an initial capacity */
/* Always remember to check return values */
mcl_string *str = mcl_string_new("Hello, world!", 512);
mcl_string_s *str = mcl_string_new("Hello, world!", 512);
if (str == NULL) {
printf("Failed to initialize string");
exit(EXIT_FAILURE);
}
/* Retrieve a C str from mcl_string with mcl_string_cstr() */
c_str = mcl_string_cstr(str);
length = mcl_string_length(str);
capacity = mcl_string_capacity(str);
printf("%s\nlength: %ld, capacity: %ld\n", c_str, length, capacity);
free(c_str);
printf("%s\nlength: %lld, capacity: %lld\n", c_str, length, capacity);
/* Append text to a mcl_string */
mcl_string_append(str, " How are you?");
@@ -26,8 +28,7 @@ int main(void) {
c_str = mcl_string_cstr(str);
length = mcl_string_length(str);
capacity = mcl_string_capacity(str);
printf("%s\nsize: %ld, cap: %ld\n", c_str, length, capacity);
free(c_str);
printf("%s\nsize: %lld, cap: %lld\n", c_str, length, capacity);
/* Always deallocate memory */
mcl_string_free(str);

View File

@@ -1,20 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include "../../string/mystring.h"
int main(void) {
mcl_string *str = mcl_string_new("Hello, world!", 0);
mcl_string_append(str, " How are you?");
char *c_str = mcl_string_cstr(str);
size_t len = mcl_string_length(str);
size_t cap = mcl_string_capacity(str);
fprintf(stdout, "%s\nlen: %ld\ncapacity: %ld\n", c_str, len, cap);
mcl_string_free(str);
free(c_str);
return 0;
}