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 33 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#define log_int(n) printf("%s == %d\n", (#n), (n))
int main(int argc, char* argv[]) {
// malloc space for 1 integer and store the address
// of the new integer in a_n
int* a_n = malloc(sizeof(*a_n));
// bad code quality, but compiles
// int* a_n = malloc(sizeof(int));
// bad code quality: not obvious why we used 4
// could cause problems on other platforms
// int* a_n = malloc(4);
// bad code quality: unneeded cast
// int* a_n = (int*)malloc(sizeof(*a_n));
// no initial value, this is random data from memory
//log_int(*a_n);
// assign a value to the address a_n
*a_n = 4;
// print assigned value
log_int(*a_n);
free(a_n);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2022 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.