Okay, so I figured out what's happening, but not sure why or how to deal with it.
I modified the code a little bit:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void){
//10 byte bank
char *main_pointer = malloc(10);
// array of pointers to 2 integers pointers (8 bytes)
int **int_pointer_array = malloc(2*sizeof(int *));
// set first integer pointer in array to point to byte 0 in 10 byte bank
int_pointer_array[0] = (int *) main_pointer;
// set the second integer pointer in array to point to byte 6 in 10 byte bank
int_pointer_array[1] = (int *) (main_pointer + 6);
// assign values
*int_pointer_array[0] = 6505;
*int_pointer_array[1] = 5;
// print values
printf("The values are %d and %i\n", *int_pointer_array[0], *int_pointer_array[1]);
printf("The values are %d and %d\n", *int_pointer_array[0], *(main_pointer+4));
int i = 0;
for (i = 0; i < 10; i++){
printf("main_ptr char byte %d, value is : %i\n", i, *(main_pointer + i));
}
printf("Pointer[0] is pointing to: %p\n", int_pointer_array[0]);
printf("Pointer[1] is pointing to: %p\n", int_pointer_array[1]);
return 0;
}
And the output of said code:
Code:
The values are 6505 and 327680
The values are 6505 and 5
main_ptr char byte 0, value is : 105
main_ptr char byte 1, value is : 25
main_ptr char byte 2, value is : 0
main_ptr char byte 3, value is : 0
main_ptr char byte 4, value is : 5
main_ptr char byte 5, value is : 0
main_ptr char byte 6, value is : 0
main_ptr char byte 7, value is : 0
main_ptr char byte 8, value is : 0
main_ptr char byte 9, value is : 0
Pointer[0] is pointing to: 0x11008
Pointer[1] is pointing to: 0x1100e
So as you can see, instead of the line *int_pointer_array[1] = 5; putting the value 5 at byte 6, it is actually putting it at byte 4, and when it prints it out via the printf, it's actually interpreting it as 0x50000, which is = 327680.
Why is this happening, and what should I do about it?
Thank you for any help.