Advertisement
sapitando

Ponteiros C - 1.

Aug 27th, 2016
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.29 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 = calloc(3,sizeof(void*));
  15.    
  16.     lpGP[0] = malloc(11 * sizeof(char));
  17.     strcpy(((char**)(lpGP))[0], "ABCDEFGHIJ");
  18. //  strcpy(&((char**)(lpGP))[0][0], "ABCDEFGHIJ");     
  19. //  strcpy((char*)(lpGP[0]), "ABCDEFGHIJ");
  20. //  strcpy(&((char*)(lpGP[0]))[0], "ABCDEFGHIJ");
  21.    
  22.    
  23.     lpGP[1] = malloc(2 * sizeof(int));
  24.    
  25.     ((int**)(lpGP))[1][0] = 250;
  26. //  *(*(((int**)(lpGP)) + 1) + 0) = 250;
  27. //  ((int*)(lpGP[1]))[0] = 250;
  28. //  *((int*)(*(lpGP + 1)) + 0) = 250;
  29. //  *(int*)((char*)(*(lpGP + 1)) + (0 * sizeof(int))) = 250;
  30.    
  31.     ((int**)(lpGP))[1][1] = 500;
  32. //  *(*(((int**)(lpGP)) + 1) + 1) = 500;   
  33. //  ((int*)(lpGP[1]))[1] = 500;
  34. //  *((int*)(*(lpGP + 1)) + 1) = 500;
  35. //  *(int*)((char*)(*(lpGP + 1)) + (1 * sizeof(int))) = 500;
  36.    
  37.    
  38.     lpGP[2] = malloc(2 * sizeof(float));
  39.    
  40.     ((float**)(lpGP))[2][0] = 5.25;
  41. //  *(*(((float**)(lpGP)) + 2) + 0) = 5.25;
  42. //  ((float*)(lpGP[2]))[0] = 5.25;
  43. //  *((float*)(*(lpGP + 2)) + 0) = 5.25;
  44. //  *(float*)((char*)(*(lpGP + 2)) + (0 * sizeof(float))) = 5.25;
  45.    
  46.     ((float**)(lpGP))[2][1] = 10.50;
  47. //  *(*(((float**)(lpGP)) + 2) + 1) = 10.50;   
  48. //  ((float*)(lpGP[2]))[1] = 10.50;
  49. //  *((float*)(*(lpGP + 2)) + 1) = 10.50;
  50. //  *(float*)((char*)(*(lpGP + 2)) + (1 * sizeof(float))) = 10.50;
  51.    
  52.        
  53.     printf("%p -> %p\n", (void*)(&lpGP), (void*)(lpGP));
  54.     puts("");
  55.    
  56.     printf("%p -> %p\n", (void*)(&lpGP[0]), lpGP[0]);
  57. //  printf("%p -> %p\n", (void*)(lpGP + 0), *(lpGP + 0));
  58.  
  59.     printf("%p -> %p\n", (void*)(&lpGP[1]), lpGP[1]);
  60. //  printf("%p -> %p\n", (void*)(lpGP + 1), *(lpGP + 1));
  61.    
  62.     printf("%p -> %p\n", (void*)(&lpGP[2]), lpGP[2]);
  63. //  printf("%p -> %p\n", (void*)(lpGP + 2), *(lpGP + 2));
  64.     puts("");
  65.    
  66.     printf("%p = %s\n", (void*)(&((char*)(lpGP[0]))[0]), &((char*)(lpGP[0]))[0]);
  67. //  printf("%p = %s\n", (void*)((char*)(*(lpGP + 0)) + 0), ((char*)(*(lpGP + 0)) + 0));
  68.     puts("");
  69.    
  70.     printf("%p = %i\n", (void*)(&((int*)(lpGP[1]))[0]), ((int*)(lpGP[1]))[0]);
  71. //  printf("%p = %i\n", (void*)((int*)(*(lpGP + 1)) + 0), *((int*)(*(lpGP + 1)) + 0));
  72. //  printf("%p = %i\n", (void*)((char*)(*(lpGP + 1)) + (0 * sizeof(int))), *(int*)((char*)(*(lpGP + 1)) + (0 * sizeof(int))));
  73.    
  74.     printf("%p = %i\n", (void*)(&((int*)(lpGP[1]))[1]), ((int*)(lpGP[1]))[1]);
  75. //  printf("%p = %i\n", (void*)((int*)(*(lpGP + 1)) + 1), *((int*)(*(lpGP + 1)) + 1));
  76. //  printf("%p = %i\n", (void*)((char*)(*(lpGP + 1)) + (1 * sizeof(int))), *(int*)((char*)(*(lpGP + 1)) + (1 * sizeof(int))));
  77.     puts("");
  78.    
  79.     printf("%p = %f\n", (void*)(&((float*)(lpGP[2]))[0]), ((float*)(lpGP[2]))[0]);
  80. //  printf("%p = %f\n", (void*)((float*)(*(lpGP + 2)) + 0), *((float*)(*(lpGP + 2)) + 0));
  81. //  printf("%p = %f\n", (void*)((char*)(*(lpGP + 2)) + (0 * sizeof(float))), *(float*)((char*)(*(lpGP + 2)) + (0 * sizeof(float))));
  82.    
  83.     printf("%p = %f\n", (void*)(&((float*)(lpGP[2]))[1]), ((float*)(lpGP[2]))[1]);
  84. //  printf("%p = %f\n", (void*)((float*)(*(lpGP + 2)) + 1), *((float*)(*(lpGP + 2)) + 1));
  85. //  printf("%p = %f\n", (void*)((char*)(*(lpGP + 2)) + (1 * sizeof(float))), *(float*)((char*)(*(lpGP + 2)) + (1 * sizeof(float))));
  86.    
  87.     getch();
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement