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 26 27 28 29 30 31 32 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
// okay to copy/adapt
// howver, first method will be a lot easier
#define log_addr(n) printf("%s == %p\n", #n, (void*)(n))
void return_string_by_address(char** a_string) {
// a_string should contain "Hi"
*a_string = malloc(sizeof(**a_string) * 3);
log_addr(*a_string);
**a_string = 'H';
*a_string += 1;
**a_string = 'i';
*a_string += 1;
**a_string = '\0';
*a_string -= 2;
}
int main(int argc, char* argv[]) {
char* result;
return_string_by_address(&result);
printf("%s\n", result);
free(result);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2024 Alexander J. Quinn & David Burnett This content is protected and may not be shared, uploaded, or distributed.