[ create a new paste ] login | about

Link: http://codepad.org/rBJzsh3X    [ raw code | output | fork | 1 comment ]

muffinman007 - C, pasted on Nov 11:
#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;

}


Output:
What's in the array main_pointer before casting:  ABCDEFGHI
What's in the array main_pointer after casting:   ABCDEFGHI

What's in the main_pointer after *int_pointer[0] = 0x64636261;:  abcdEFGHI

What's in the main_pointer after *int_pointer[1] = 0x34333231;:  abcdEF1234

The memory address of main_pointer or (main_pointer + 0):   134520952
What's inside/What is at int_pointer:                       134520952

What's inside the array main_pointer at *(main_pointer + 0): a
what's the value of element main_pointer[0]:                 a
what inside the array at *(main_pointer + 0) as a digit using (int):  97
what the value of element main_pointer[0] as a digit using (int):     97

What's at the memory address of **int_pointer:   1684234849
which is the same as 64636261 in hex or abcd in ASCII

what's at the memory address of int_pointer:                134520952
The memory address of int_pointer:        134520992
The memory address of (int_pointer + 1):  134520996
How far away is (int_pointer + 1) to int_pointer: 4 bytes

The memory address of main pointer + 6:           134520958
What's at the memory address of *int_pointer[1]:  875770417
Which is the same as 34333231 in hex or 4321 in ASCII.



Create a new paste based on this one


Comments:
posted by muffinman007 on Nov 11
line 20 has a minor error:
fixed:
printf("How far away is (int_pointer + 1) to int_pointer: %d bytes\n", (char*)(int_pointer + 1) - (char*)(int_pointer));

char is supposed to be char*
reply