Skip to content

Instantly share code, notes, and snippets.

@agagniere
Last active January 16, 2023 14:41
Show Gist options
  • Save agagniere/666a141b24586fa39a8e13218071be5a to your computer and use it in GitHub Desktop.
Save agagniere/666a141b24586fa39a8e13218071be5a to your computer and use it in GitHub Desktop.
Demonstrate
#include <stdio.h>
#define C_ARRAY_LENGTH(A) (sizeof(A) / sizeof(*(A)))
void print_memory(const void* addr, size_t size);
const char config[][8] = {
"Hello",
"Salut",
"Hola",
"Gutentag",
"Nihao"
};
int main()
{
const size_t length = C_ARRAY_LENGTH(config);
for (int i = 0; i < length; i++)
printf("%.8s\n", config[i]);
printf("\n");
print_memory(config, sizeof(config));
printf("\n");
if (0)
{ /* Segfaults */
char** fail = (char**)config;
for (int i = 0; i < length; i++)
printf("%.8s\n", fail[i]);
}
else
for (char* line = (char*)config; line < (char*)config + sizeof(config); line += sizeof(*config))
printf("%.8s\n", line);
return 0;
}
@agagniere
Copy link
Author

agagniere commented Jan 16, 2023

$ cat Makefile 
demo_array2d: print_memory.o

clean:
	$(RM) demo_array2d *.o
$ make
cc    -c -o print_memory.o print_memory.c
cc     demo_array2d.c print_memory.o   -o demo_array2d
$ ./demo_array2d 
Hello
Salut
Hola
Gutentag
Nihao

4865 6c6c 6f00 0000 5361 6c75 7400 0000 Hello...Salut...
486f 6c61 0000 0000 4775 7465 6e74 6167 Hola....Gutentag
4e69 6861 6f00 0000                     Nihao...

Hello
Salut
Hola
Gutentag
Nihao

@agagniere
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment