D
dr.oktopus
Hello,
here I am talking about having good habits in code writing.
I have a simple problem: have two messages that represent
the same string, one in lowercase and the other in uppercase
letter. Under some circustamces, I want to print the first or
the second, so I couple them inside an array and use a
variable "allcaps" as index.
And I have to precompute the length of the strings, that are CONSTANT
strings.
A solution is:
const char *messages[2] = { "this is lowercase string", "this is
uppercase string" };
and
#define MESSAGE_LEN (strlen(messages[0])
But what if you prefer, as good habits could suggest, to rewrite
all using only arrays and the sizeof operator?
const char message_lo[] = "this is lowercase";
const char message_up[] = "this is uppercase";
#define MESSAGE_LEN (sizeof(message_lo) - 1)
const char messages[2][MESSAGE_LEN] = { &message_lo, &message_hi };
What is better? (I think the answer is: "none")
Thanks,
willy
here I am talking about having good habits in code writing.
I have a simple problem: have two messages that represent
the same string, one in lowercase and the other in uppercase
letter. Under some circustamces, I want to print the first or
the second, so I couple them inside an array and use a
variable "allcaps" as index.
And I have to precompute the length of the strings, that are CONSTANT
strings.
A solution is:
const char *messages[2] = { "this is lowercase string", "this is
uppercase string" };
and
#define MESSAGE_LEN (strlen(messages[0])
But what if you prefer, as good habits could suggest, to rewrite
all using only arrays and the sizeof operator?
const char message_lo[] = "this is lowercase";
const char message_up[] = "this is uppercase";
#define MESSAGE_LEN (sizeof(message_lo) - 1)
const char messages[2][MESSAGE_LEN] = { &message_lo, &message_hi };
What is better? (I think the answer is: "none")
Thanks,
willy