Created
March 7, 2023 22:31
-
-
Save pavly-gerges/9d739c6099532a90a649b95ce0c59e6a to your computer and use it in GitHub Desktop.
Tests Multi-dimensional arrays and accessing memory chunks using pointers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(void*) { | |
/* create an array of strings (arrays of characters) */ | |
/* This buffer is allocated in contigous memory chunks */ | |
const char credentials[][255] = {"Andrew", | |
"ZirqfRopWpedsS0012#24"}; | |
/* get an element from the 1st dimension */ | |
const char* user_cred = credentials[0]; /* or use *(credentials + 0) */ | |
const char* passwd_cred = credentials[1]; /* or use *(credentials + 1) */ | |
printf("%s\n", user_cred); | |
printf("%s\n", passwd_cred); | |
/* get an element from the 2nd dimension */ | |
const char user_cred_c0 = user_cred[0]; | |
const char user_cred_c1 = credentials[0][1]; | |
const char user_cred_c2 = *(user_cred + 2); | |
const char user_cred_c3 = *(*(credentials + 0) + 3); | |
const char user_cred_c4 = *(*(credentials + 0) + 4); | |
/* HACK !! Convert into a single buffer */ | |
const char user_cred_c5 = *(((const char*) credentials) + 5); | |
printf("%c", user_cred_c0); | |
printf("%c", user_cred_c1); | |
printf("%c", user_cred_c2); | |
printf("%c", user_cred_c3); | |
printf("%c", user_cred_c4); | |
printf("%c", user_cred_c5); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment