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 34 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#define log_int(n) printf("%s == %d\n", #n, (n))
#define log_char(n) printf("%s == '%c'\n", #n, (n))
#define log_addr(n) printf("%s == %p\n", #n, (void*)(n))
int main(int argc, char* argv[]) {
char* str = "Hello World";
int str_len = 0; // using loop index for size later
for (; str[str_len] != '\0'; str_len++) {
log_char(str[str_len]);
log_addr(str + str_len);
}
log_int(str_len); // logical length without the null
// character
char* str_pos = str;
for (; *str_pos != '\0'; str_pos++) {
log_char(*str_pos);
log_addr(str_pos);
}
// this gives us some extra validation at the cost
// of being slightly longer
int str_len2 = str_pos - str;
log_int(str_len2);
// we could have done this, but less validation
log_int((int)(str_pos - str));
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.