Note: I'm getting back into C after a 10 year hiatus.
I'm trying to demonstrate that an array of characters is not terminated by a '\0'. To accomplish this I'm using a gcc C extension packed to make sure that the following bytes after my array is known. I use a unsigned value of UINT_MAX to make sure the following bytes are not zero and 0 to back sure the the values are zero.
Here's the what I'm trying to demonstrate. I know that an array of characters does not append a '\0' to the end but I'd like to show that.
To demonstrate that array is not terminated by '\0' I follow the array by UINT_MAX in this code and get a strlen greater than the array.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define ARR_SIZE 5
struct mys
{
char name[ARR_SIZE];
unsigned n;
} __attribute__((__packed__));
int main(int argc, char **argv)
{
struct mys s = {{'G', '4', '1', '4', '3'}, UINT_MAX};
fprintf(stdout, "Length: %ld\n", strlen(s.name));
fprintf(stdout, "%s\n", s.name);
return 0;
}
The output from the above code is:
Now if I change the value UINT_MAX to 0
Code:
include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define ARR_SIZE 5
struct mys
{
char name[ARR_SIZE];
unsigned n;
} __attribute__((__packed__));
int main(int argc, char **argv)
{
struct mys s = {{'G', '4', '1', '4', '3'}, 0};
fprintf(stdout, "Length: %ld\n", strlen(s.name));
fprintf(stdout, "%s\n", s.name);
return 0;
}
I get an output:
Now what I'd like to know is... Does this actually demonstrate, since I'm using a C extension, that an array of characters doesn't append a '\0' to the end?