-
Notifications
You must be signed in to change notification settings - Fork 0
/
11-challenge.c
76 lines (67 loc) · 2.51 KB
/
11-challenge.c
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 20
/*
* Program 11 : Challenge - Write a function that displays the memory of an array
* The function will receive an integer array and its size then print out
* a table to the screen which looks like this:
*
* Idx Mem Adr b1 b2 b3 b4
* --------------
* [000] | 0x02e192c9 | 0x16b4f7468 c9 92 e1 02
* --------------
* [001] | 0x276bcb99 | 0x16b4f746c 99 cb 6b 27
* --------------
* [002] | 0x1603be07 | 0x16b4f7470 07 be 03 16
* --------------
* [003] | 0x4fb0c8db | 0x16b4f7474 db c8 b0 4f
* --------------
*
* Column 1 (Idx) - the index of the element
* Column 2 (Mem) - the content of the 4 byte block starting at the address
* of the index in hexidecimal (printf format %x)
* Column 3 (Adr) - the address of the first byte of the 4 byte block
* Colunn 4 (b1-b4) - the contents of each byte of the 4 byte integer in hex
* where byte 1 is the byte addressed by the address of the
* index.
*/
/*
* Function printmem
* This function takes an integer array and the size of the array.
* It prints a memory table to the screen.
*/
void printmem (int a[], int size){
printf(" Idx Mem Adr b1 b2 b3 b4\n");
printf(" --------------\n");
for(int i = 0; i < size; i++){
/*
* For each element of the int array, initialize a pointer with the
* address of the index. Cast to a 1 byte pointer (an unsigned char).
* Use pointer arithmetic to get the address of each component byte of
* the 4-byte integer. Then dereference to get the value stored in
* that byte.
*/
unsigned char* ptr = (unsigned char*) &a[i];
unsigned char byte1 = *ptr;
unsigned char byte2 = *(ptr+1);
unsigned char byte3 = *(ptr+2);
unsigned char byte4 = *(ptr+3);
printf("[%03i] | 0x%08x | %p %02x %02x %02x %02x\n",
i, a[i], &a[i], byte1, byte2, byte3, byte4);
printf(" --------------\n");
}
printf("\n");
}
int main(){
/* Construct and initialize an array of size "SIZE" */
int a[SIZE] = {0};
srand(time(NULL));
/* Loop and initialize the array with random numbers. */
for(int i = 0; i < SIZE; i++){
a[i] = rand();
}
/* Call the printmem function */
printmem(a, SIZE);
return 1;
}