Advertisement
sapitando

Ponteiros C - 2.

Aug 27th, 2016
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.00 KB | None | 0 0
  1. /* Autor : Tiago Portela
  2.    Email : sapitando@gmail.com
  3.    Sobre : Compilado com GCC e LCC.
  4.    Obs : Apenas tentando aprender algoritimos, sozinho, por hobby. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <conio.h>
  11.  
  12. int main(void){
  13.  
  14.     void *lpGP;
  15.     lpGP = calloc(3,sizeof(void*));
  16.  
  17.     ((void**)(lpGP))[0] = malloc(11 * sizeof(char));
  18.     strcpy(((char**)(lpGP))[0], "ABCDEFGHIJ");
  19.     strcpy(&((char**)(lpGP))[0][0], "ABCDEFGHIJ"); 
  20.     strcpy(&((char*)(((void**)(lpGP))[0]))[0], "ABCDEFGHIJ");
  21.     strcpy((char*)(((void**)(lpGP))[0]), "ABCDEFGHIJ");
  22.  
  23.     ((void**)(lpGP))[1] = malloc(2 * sizeof(int));
  24.     ((int**)(lpGP))[1][0] = 250;
  25.     *(*(((int**)(lpGP)) + 1) + 0) = 250;
  26.     ((int*)(((void**)(lpGP))[1]))[0] = 250;
  27.     *((int*)(*(((void**)(lpGP)) + 1)) + 0) = 250;
  28.     *((int*)((char*)(*(((void**)(lpGP)) + 1)) + (0 * sizeof(int)))) = 250;
  29.     ((int**)(lpGP))[1][1] = 500;
  30.     *(*(((int**)(lpGP)) + 1) + 1) = 500;
  31.     ((int*)(((void**)(lpGP))[1]))[1] = 500;
  32.     *((int*)(*(((void**)(lpGP)) + 1)) + 1) = 500;
  33.     *((int*)((char*)(*(((void**)(lpGP)) + 1)) + (1 * sizeof(int)))) = 500;
  34.  
  35.     ((void**)(lpGP))[2] = malloc(2 * sizeof(float));
  36.     ((float**)(lpGP))[2][0] = 5.25;
  37.     *(*(((float**)(lpGP)) + 2) + 0) = 5.25;
  38.     ((float*)(((void**)(lpGP))[2]))[0] = 5.25;
  39.     *((float*)(*(((void**)(lpGP)) + 2)) + 0) = 5.25;
  40.     *((float*)((char*)(*(((void**)(lpGP)) + 2)) + (0 * sizeof(float)))) = 5.25;
  41.     ((float**)(lpGP))[2][1] = 10.50;
  42.     *(*(((float**)(lpGP)) + 2) + 1) = 10.50;
  43.     ((float*)(((void**)(lpGP))[2]))[1] = 10.50;
  44.     *((float*)(*(((void**)(lpGP)) + 2)) + 1) = 10.50;
  45.     *((float*)((char*)(*(((void**)(lpGP)) + 2)) + (1 * sizeof(float)))) = 10.50;
  46.  
  47.  
  48.     printf("%p -> %p\n", (void*)(&lpGP), (void*)(lpGP));
  49.     puts("");    
  50.  
  51.     printf("%p -> %p\n", (void*)(&((void**)(lpGP))[0]), ((void**)(lpGP))[0]);
  52.     printf("%p -> %p\n", (void*)(((void**)(lpGP)) + 0), *(((void**)(lpGP)) + 0));
  53.     puts("");
  54.  
  55.     printf("%p -> %p\n", (void*)(&((void**)(lpGP))[1]), ((void**)(lpGP))[1]);
  56.     printf("%p -> %p\n", (void*)(((void**)(lpGP)) + 1), *(((void**)(lpGP)) + 1));
  57.     puts("");
  58.  
  59.     printf("%p -> %p\n", (void*)(&((void**)(lpGP))[2]), ((void**)(lpGP))[2]);
  60.     printf("%p -> %p\n", (void*)(((void**)(lpGP)) + 2), *(((void**)(lpGP)) + 2));
  61.     puts("");
  62.  
  63.     printf("%p = %s\n", (void*)(&((char*)(((void**)(lpGP))[0]))[0]), &((char*)(((void**)(lpGP))[0]))[0]);
  64.     printf("%p = %s\n", (void*)((char*)(*(((void**)(lpGP)) + 0)) + 0), ((char*)(*(((void**)(lpGP)) + 0)) + 0));
  65.     puts("");
  66.  
  67.     printf("%p = %i\n", (void*)(&((int*)(((void**)(lpGP))[1]))[0]), ((int*)(((void**)(lpGP))[1]))[0]);
  68.     printf("%p = %i\n", (void*)((int*)(*(((void**)(lpGP)) + 1)) + 0), *((int*)(*(((void**)(lpGP)) + 1)) + 0));
  69.     printf("%p = %i\n", (void*)((char*)(*(((void**)(lpGP)) + 1)) + (0 * sizeof(int))), *((int*)((char*)(*(((void**)(lpGP)) + 1)) + (0 * sizeof(int)))));
  70.  
  71.     printf("%p = %i\n", (void*)(&((int*)(((void**)(lpGP))[1]))[1]), ((int*)(((void**)(lpGP))[1]))[1]);
  72.     printf("%p = %i\n", (void*)((int*)(*(((void**)(lpGP)) + 1)) + 1), *((int*)(*(((void**)(lpGP)) + 1)) + 1));
  73.     printf("%p = %i\n", (void*)((char*)(*(((void**)(lpGP)) + 1)) + (1 * sizeof(int))), *((int*)((char*)(*(((void**)(lpGP)) + 1)) + (1 * sizeof(int)))));
  74.     puts("");
  75.  
  76.     printf("%p = %f\n", (void*)(&((float*)(((void**)(lpGP))[2]))[0]), ((float*)(((void**)(lpGP))[2]))[0]);
  77.     printf("%p = %f\n", (void*)((float*)(*(((void**)(lpGP)) + 2)) + 0), *((float*)(*(((void**)(lpGP)) + 2)) + 0));
  78.     printf("%p = %f\n", (void*)((char*)(*(((void**)(lpGP)) + 2)) + (0 * sizeof(float))), *((float*)((char*)(*(((void**)(lpGP)) + 2)) + (0 * sizeof(float)))));
  79.  
  80.     printf("%p = %f\n", (void*)(&((float*)(((void**)(lpGP))[2]))[1]), ((float*)(((void**)(lpGP))[2]))[1]);
  81.     printf("%p = %f\n", (void*)((float*)(*(((void**)(lpGP)) + 2)) + 1), *((float*)(*(((void**)(lpGP)) + 2)) + 1));
  82.     printf("%p = %f\n", (void*)((char*)(*(((void**)(lpGP)) + 2)) + (1 * sizeof(float))), *((float*)((char*)(*(((void**)(lpGP)) + 2)) + (1 * sizeof(float)))));
  83.  
  84.     getch();
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement