[ create a new paste ] login | about

Link: http://codepad.org/WkodWcO3    [ raw code | output | fork ]

C, pasted on Sep 21:
#include <stdio.h>

int main()
{
    printf("------------------------\n");
    int n = 1000;
    char * p = (char*) &n;
    printf("%x \n", p);

    p = (char*) n;
    printf("%d \n", p);

    printf("------------------------\n");
    int i;
    int k;
    int pos = 0;

    int x;
    int y;

    int size = sizeof(int);
    int  * xy = (int *) malloc (2* 3* size);

    memset(xy, 0x00, 2*3*size);

    for(y=0; y<3; y++)
    {
        for(x=0; x<2; x++)
        {
           *(xy+pos) = pos+1000;
            printf("xy+pos  : %x      pos : %d     *(xy+pos) : %d \t\t", xy+pos, pos, *(xy+pos));

            pos = pos + size;
         }
        printf("\n");
    }

    printf("------------------------\n");
//안됨.
//  char * z = (char*) malloc (5);
//  z+1 = xy;

    printf("------------------------\n");
    int max = pos;
    char* z[5];

    for(i=0; i<5; i++)
    {
        z[i] = xy;
        int * p = (int*) z[i];
//        p = (int*) xy;

        pos = 0;
        for(y=0; y<3; y++)
        {
            for(x=0; x<2; x++)
            {
                printf("%x : %d \t", p+pos, *(p+pos));
                pos = pos + size;
            }
            printf("\t\t");
        }
        printf("\n");
    }

    printf("------------------------\n");
    int a_xy[2][3];
    for(i=0; i<5; i++)
    {
        int * p = (int*) z[i];

        pos = 0;
        for(y=0; y<3; y++)
        {
            for(x=0; x<2; x++)
            {
                printf("%x : %d \t", p+pos, *(p+pos));
                pos = pos + size;
            }
            printf("\t\t");
        }
        printf("\n");
    }

    printf("------------------------\n");

    for(i=0; i<5; i++)
    {
        int * p = (int*) z[i];

        pos = 0;
        for(y=0; y<3; y++)
        {
            for(x=0; x<2; x++)
            {
                a_xy[x][y] = *(p+pos);

                printf("%d \t", a_xy[x][y]);
                pos = pos + size;
            }
            printf("\t\t");
        }
        printf("\n");
    }

    printf("------------------------\n");
    int tpos = 0;
    for(i=0; i<5; i++)
    {
        int * p = (int*) z[i];
        printf("%x \n", z[i]);

        memcpy(&a_xy[0][0], p+tpos, 2*3);
//        tpos = tpos + 2*3*size;  안됨.
        tpos = tpos + 2*3;

        for(y=0; y<3; y++)
        {
            for(x=0; x<2; x++)
            {
                printf("%d \t", a_xy[x][y]);
            }
        }
        printf("\n");
    }


    free(xy);

    return 0;
}


Output:
------------------------
ff8c6fd4 
1000 
------------------------
xy+pos  : 91f0078      pos : 0     *(xy+pos) : 1000 		xy+pos  : 91f0088      pos : 4     *(xy+pos) : 1004 		
xy+pos  : 91f0098      pos : 8     *(xy+pos) : 1008 		xy+pos  : 91f00a8      pos : 12     *(xy+pos) : 1012 		
xy+pos  : 91f00b8      pos : 16     *(xy+pos) : 1016 		xy+pos  : 91f00c8      pos : 20     *(xy+pos) : 1020 		
------------------------
------------------------
91f0078 : 1000 	91f0088 : 1004 			91f0098 : 1008 	91f00a8 : 1012 			91f00b8 : 1016 	91f00c8 : 1020 			
91f0078 : 1000 	91f0088 : 1004 			91f0098 : 1008 	91f00a8 : 1012 			91f00b8 : 1016 	91f00c8 : 1020 			
91f0078 : 1000 	91f0088 : 1004 			91f0098 : 1008 	91f00a8 : 1012 			91f00b8 : 1016 	91f00c8 : 1020 			
91f0078 : 1000 	91f0088 : 1004 			91f0098 : 1008 	91f00a8 : 1012 			91f00b8 : 1016 	91f00c8 : 1020 			
91f0078 : 1000 	91f0088 : 1004 			91f0098 : 1008 	91f00a8 : 1012 			91f00b8 : 1016 	91f00c8 : 1020 			
------------------------
91f0078 : 1000 	91f0088 : 1004 			91f0098 : 1008 	91f00a8 : 1012 			91f00b8 : 1016 	91f00c8 : 1020 			
91f0078 : 1000 	91f0088 : 1004 			91f0098 : 1008 	91f00a8 : 1012 			91f00b8 : 1016 	91f00c8 : 1020 			
91f0078 : 1000 	91f0088 : 1004 			91f0098 : 1008 	91f00a8 : 1012 			91f00b8 : 1016 	91f00c8 : 1020 			
91f0078 : 1000 	91f0088 : 1004 			91f0098 : 1008 	91f00a8 : 1012 			91f00b8 : 1016 	91f00c8 : 1020 			
91f0078 : 1000 	91f0088 : 1004 			91f0098 : 1008 	91f00a8 : 1012 			91f00b8 : 1016 	91f00c8 : 1020 			
------------------------
1000 	1004 			1008 	1012 			1016 	1020 			
1000 	1004 			1008 	1012 			1016 	1020 			
1000 	1004 			1008 	1012 			1016 	1020 			
1000 	1004 			1008 	1012 			1016 	1020 			
1000 	1004 			1008 	1012 			1016 	1020 			
------------------------
91f0078 
1000 	1004 	0 	1012 	1016 	1020 	
91f0078 
215 	1004 	3953 	1012 	1016 	1020 	
91f0078 
1012 	1004 	0 	1012 	1016 	1020 	
91f0078 
0 	1004 	0 	1012 	1016 	1020 	
91f0078 
0 	1004 	0 	1012 	1016 	1020 	


Create a new paste based on this one


Comments: