codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include<stdio.h> #include<stdlib.h> #include<string.h> int main(void){ // *main_pointer is a pointer designed to look at memory in char-sized chunks. It is always our primary pointer char *main_pointer = malloc(10); // creating an array 10 spaces, char is a data type with one byte in value, so array is 10 bytes. strcpy( (main_pointer + 0), "ABCDEFGHI"); //let's you see what's inside the array main_pointer. output is "ABCDEFGH" int i; printf("What's in the array main_pointer before casting: "); for(i=0; i < 10; i++){ printf("%c", *(main_pointer + i)); } // creating our array of pointers for pointers. int **int_pointer = malloc(2 * sizeof(int *)); //assinging pointers to our array of pointers *(int_pointer) = (int *)(main_pointer + 0); int_pointer[1] = (int *)(main_pointer + 6); // this can be written as *(int_pointer + 1) = (int *)(main_pointer + 6); //let's you see what's inside the array main_pointer up to this point. output is "ABCDEFH" printf("\nWhat's in the array main_pointer after casting: "); for(i=0; i < 10; i++){ printf("%c", *(main_pointer + i)); } printf("\n"); //assigning new values into the array main_pointer using our array of pointers. *int_pointer[0] = 0x64636261; //let's you see what's inside main_pointer after assinging new value from the line above. printf("\nWhat's in the main_pointer after *int_pointer[0] = 0x64636261;: "); for(i=0; i < 10; i++){ printf("%c", *(main_pointer + i)); } printf("\n"); ////assigning new values into the array main_pointer using our array of pointers. *int_pointer[1] = 0x34333231; //let's you see what's inside main_pointer after assinging new value from the line above. printf("\nWhat's in the main_pointer after *int_pointer[1] = 0x34333231;: "); for(i=0; i < 10; i++){ printf("%c", *(main_pointer + i)); } printf("\n"); printf("\nThe memory address of main_pointer or (main_pointer + 0): %d", main_pointer); printf("\nWhat's inside/What is at int_pointer: %d\n", *(int_pointer)); printf("\nWhat's inside the array main_pointer at *(main_pointer + 0): %c", *(main_pointer + 0)); printf("\nwhat's the value of element main_pointer[0]: %c\n", main_pointer[0]); printf("what inside the array at *(main_pointer + 0) as a digit using (int): %d\n", (int)*(main_pointer + 0)); printf("what the value of element main_pointer[0] as a digit using (int): %d\n", (int)main_pointer[0]); printf("\nWhat's at the memory address of **int_pointer: %d\n", **int_pointer); printf("which is the same as 64636261 in hex or abcd in ASCII\n\n"); printf("what's at the memory address of int_pointer: %d\n", *int_pointer); printf("The memory address of int_pointer: %d\n", int_pointer); printf("The memory address of (int_pointer + 1): %d\n", (int_pointer + 1)); printf("How far away is (int_pointer + 1) to int_pointer: %d bytes\n", (char)(int_pointer + 1) - (char)(int_pointer)); printf("\nThe memory address of main pointer + 6: %d", (main_pointer + 6)); printf("\nWhat's at the memory address of *int_pointer[1]: %d", *int_pointer[1]); printf("\nWhich is the same as 34333231 in hex or 4321 in ASCII.\n\n"); free(main_pointer); free(int_pointer); return 0; }
Private
[
?
]
Run code
Submit