RicardasSim

learning pointers to arrays

Mar 19th, 2019 (edited)
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.08 KB | None | 0 0
  1. /*
  2.  * Learning pointers to arrays
  3.  *
  4.  * -std=c99 -Wall -Wextra -Wpedantic -Wshadow
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <stdbool.h>
  11. #include <string.h>
  12.  
  13. int** g_pA = NULL;
  14. unsigned int g_uiE = 0;
  15.  
  16. void cleanup()
  17. {
  18.    
  19.     if(g_uiE)
  20.     {
  21.         for(unsigned int i = 0; i < g_uiE; ++i)
  22.         {
  23.             printf("(cleanup) address of g_pA[%d]: %p\n", i, (void*) &g_pA[i]);
  24.             printf("(cleanup) value of g_pA[%d]: %p\n", i, (void*) g_pA[i]);
  25.             printf("(cleanup) free g_pA[%d]\n",i);
  26.             free(g_pA[i]);
  27.         }
  28.     }
  29.  
  30.     if(g_pA) free(g_pA);
  31.    
  32. }
  33.  
  34. bool init(int*** a, unsigned int* e)
  35. {
  36.  
  37.     int** arrTmp = NULL;
  38.    
  39.     printf("(init 1) a: %p\n", (void*)a);
  40.    
  41.     printf("(init 2) *e: %d\n",*e);
  42.    
  43.  
  44.     *a = realloc(*a, sizeof(int*)*(*e+1));
  45.    
  46.     if(!*a) return false;
  47.    
  48.     (*a)[*e] = malloc(sizeof(int)*10);
  49.    
  50.     if(!(*a)[*e]) return false;
  51.    
  52.     printf("(init 3) *a: %p\n", (void*)*a);
  53.     printf("(init 4) (*a)[*e]: %p\n", (void*)(*a)[*e]);
  54.    
  55.     (*e)++;
  56.    
  57.    
  58.    
  59.     printf("(init 5) *e: %d\n",*e);
  60.    
  61.     arrTmp = realloc(*a, sizeof(int*)*(*e+1));
  62.    
  63.     if(!arrTmp) return false;
  64.    
  65.     *a = arrTmp;
  66.    
  67.     (*a)[*e] = malloc(sizeof(int)*20);
  68.    
  69.     if(!(*a)[*e]) return false;
  70.    
  71.     printf("(init 6) *a: %p\n", (void*)*a);
  72.     printf("(init 7) (*a)[*e]: %p\n", (void*)(*a)[*e]);
  73.    
  74.     (*e)++;
  75.  
  76.  
  77.  
  78.     printf("(init 8) *e: %d\n",*e);
  79.  
  80.     arrTmp = realloc(*a, sizeof(int*)*(*e+1));
  81.    
  82.     if(!arrTmp) return false;
  83.    
  84.     *a = arrTmp;
  85.    
  86.     (*a)[*e] = malloc(sizeof(int)*30);
  87.    
  88.     if(!(*a)[*e]) return false;
  89.  
  90.     printf("(init 9) *a: %p\n", (void*)*a);
  91.     printf("(init 10) (*a)[*e]: %p\n", (void*)(*a)[*e]);    
  92.    
  93.     (*e)++;    
  94.    
  95.     printf("(init 11) *e: %d\n",*e);
  96.  
  97.    
  98.     return true;
  99.  
  100. }
  101.  
  102.  
  103. int main (int argc, char **argv){
  104.    
  105. int z[10] = {0};
  106.  
  107. int* ip;
  108.  
  109. ip=&z[0];
  110.  
  111. *ip=1;
  112.  
  113. printf("sizeof int %zu\n",sizeof(int));
  114. printf("sizeof int* %zu\n\n",sizeof(int*));
  115.  
  116.  
  117. {
  118.     int* arr = z;
  119.    
  120.     int* tmp;
  121.    
  122.     tmp = arr;
  123.     printf("(1) address of tmp: %p\n", (void*) tmp);
  124.     printf("(2) address of tmp[0]: %p\n", (void*) &tmp[0]);
  125.    
  126.     //tmp[i] = x;
  127.    
  128.     tmp += 1; //adds sizeof(int) to address
  129.     printf("(3) address of tmp: %p\n", (void*) tmp);
  130.  
  131.     *tmp = 2;
  132.    
  133.     tmp++;  //adds sizeof(int) to address
  134.     printf("(4) address of tmp: %p\n", (void*) tmp);
  135.  
  136.     *tmp = 3;
  137.    
  138.     // address of tmp is incremented two times by sizeof(int)
  139.     printf("(5) address of tmp[0]: %p\n", (void*) &tmp[0]);
  140.     printf("(6) address of tmp[1]: %p\n", (void*) &tmp[1]);
  141.     printf("(7) address of tmp[2]: %p\n\n", (void*) &tmp[2]);
  142.     tmp[2] = 5;
  143.    
  144. }
  145.  
  146. printf("%d\n",z[0]);
  147. printf("%d\n",z[1]);
  148. printf("%d\n",z[2]);
  149. printf("%d\n",z[3]);
  150. printf("%d\n\n",z[4]);
  151.  
  152.  
  153. {
  154.    
  155.     unsigned int el[] = {10,20};
  156.    
  157.     unsigned int* pel;
  158.    
  159.     unsigned int *a, *b;
  160.    
  161.     pel = el;
  162.  
  163.     //notes:
  164.     //wrong: (void*)pel+1 warning: pointer of type ‘void *’ used in arithmetic, and plus 1 byte
  165.     //(void*)(pel+1) pel is (unsigned int*), plus sizeof(unsigned int)
  166.    
  167.     printf("(pel)(1) address pel: %p\n",(void*)pel);
  168.     printf("(pel)(1) address pel+1: %p\n",(void*)(pel+1));  //plus sizeof(unsigned int)
  169.     printf("(pel)(1) the difference (in bytes): %ld\n\n",((pel+1)-pel)*sizeof(unsigned int));
  170.    
  171.    
  172.     printf("(pel)(2) address pel: %p\n",(void*)pel);
  173.     printf("(pel)(2) address pel++: %p\n",(void*)pel++); //postfix
  174.     printf("(pel)(2) address pel: %p\n\n",(void*)pel);
  175.    
  176.     pel = el;
  177.    
  178.     printf("(pel)(3) address ++pel: %p\n",(void*)++pel);
  179.     printf("(pel)(3) address pel: %p\n\n",(void*)pel);
  180.    
  181.     pel = el;
  182.      
  183.     printf("(pel)(4) address &pel[0]: %p\n",(void*)&pel[0]);
  184.     printf("(pel)(4) address &pel[1]: %p\n",(void*)&pel[1]);
  185.     printf("(pel)(4) the difference (in bytes): %ld\n\n",(&pel[1]-&pel[0])*sizeof(unsigned int));
  186.    
  187.     a = pel;
  188.     b = pel+1;
  189.    
  190.     printf("(pel)(5) address a: %p\n",(void*)a);
  191.     printf("(pel)(5) address b: %p\n",(void*)b);
  192.     printf("(pel)(5) the difference (in bytes): %ld\n\n",(b-a)*sizeof(unsigned int));    
  193.    
  194.     printf("(pel)(6) address pel: %p\n",(void*)pel);
  195.     printf("(pel)(6) pel[0]: %d\n",pel[0]);
  196.     printf("(pel)(6) *pel: %d\n",*pel);
  197.     printf("(pel)(6) (*pel)++: %d\n",(*pel)++);
  198.     printf("(pel)(6) *pel: %d\n",*pel);
  199.     printf("(pel)(6) *pel++: %d\n",*pel++); //postfix
  200.     printf("(pel)(6) *pel: %d\n\n",*pel);
  201.    
  202.     printf("(pel)(7) *el: %d\n",*el);
  203.     printf("(pel)(7) *el+1: %d\n",*el+1);
  204.     printf("(pel)(7) (*el)+1: %d\n",(*el)+1);
  205.     printf("(pel)(7) *(el+1): %d\n\n",*(el+1));
  206.  
  207. }
  208.  
  209.  
  210. if(!init(&g_pA,&g_uiE))
  211. {
  212.     cleanup();
  213.     return 1;
  214. }
  215.  
  216. printf("init OK. !\n");
  217.  
  218. printf("g_uiE: %d\n",g_uiE);
  219.  
  220. //just testing
  221. memset(g_pA[0], 0, 10*sizeof(unsigned int));
  222. memset(g_pA[1], 0, 20*sizeof(unsigned int));
  223. memset(g_pA[2], 0, 30*sizeof(unsigned int));
  224.  
  225. g_pA[0][3] = 123;
  226.  
  227. printf("%d\n",g_pA[0][3]);
  228.  
  229. cleanup();
  230.  
  231. return 0;
  232. }
  233.  
  234. /*
  235.  
  236. output:
  237.  
  238. sizeof int 4
  239. sizeof int* 8
  240.  
  241. (1) address of tmp: 0x7ffd942690b0
  242. (2) address of tmp[0]: 0x7ffd942690b0
  243. (3) address of tmp: 0x7ffd942690b4
  244. (4) address of tmp: 0x7ffd942690b8
  245. (5) address of tmp[0]: 0x7ffd942690b8
  246. (6) address of tmp[1]: 0x7ffd942690bc
  247. (7) address of tmp[2]: 0x7ffd942690c0
  248.  
  249. 1
  250. 2
  251. 3
  252. 0
  253. 5
  254.  
  255. (pel)(1) address pel: 0x7ffd942690a8
  256. (pel)(1) address pel+1: 0x7ffd942690ac
  257. (pel)(1) the difference (in bytes): 4
  258.  
  259. (pel)(2) address pel: 0x7ffd942690a8
  260. (pel)(2) address pel++: 0x7ffd942690a8
  261. (pel)(2) address pel: 0x7ffd942690ac
  262.  
  263. (pel)(3) address ++pel: 0x7ffd942690ac
  264. (pel)(3) address pel: 0x7ffd942690ac
  265.  
  266. (pel)(4) address &pel[0]: 0x7ffd942690a8
  267. (pel)(4) address &pel[1]: 0x7ffd942690ac
  268. (pel)(4) the difference (in bytes): 4
  269.  
  270. (pel)(5) address a: 0x7ffd942690a8
  271. (pel)(5) address b: 0x7ffd942690ac
  272. (pel)(5) the difference (in bytes): 4
  273.  
  274. (pel)(6) address pel: 0x7ffd942690a8
  275. (pel)(6) pel[0]: 10
  276. (pel)(6) *pel: 10
  277. (pel)(6) (*pel)++: 10
  278. (pel)(6) *pel: 11
  279. (pel)(6) *pel++: 11
  280. (pel)(6) *pel: 20
  281.  
  282. (pel)(7) *el: 11
  283. (pel)(7) *el+1: 12
  284. (pel)(7) (*el)+1: 12
  285. (pel)(7) *(el+1): 20
  286.  
  287. (init 1) a: 0x5556eeb60018
  288. (init 2) *e: 0
  289. (init 3) *a: 0x5556ef98d670
  290. (init 4) (*a)[*e]: 0x5556ef98d690
  291. (init 5) *e: 1
  292. (init 6) *a: 0x5556ef98d670
  293. (init 7) (*a)[*e]: 0x5556ef98d6c0
  294. (init 8) *e: 2
  295. (init 9) *a: 0x5556ef98d670
  296. (init 10) (*a)[*e]: 0x5556ef98d720
  297. (init 11) *e: 3
  298. init OK. !
  299. g_uiE: 3
  300. 123
  301. (cleanup) address of g_pA[0]: 0x5556ef98d670
  302. (cleanup) value of g_pA[0]: 0x5556ef98d690
  303. (cleanup) free g_pA[0]
  304. (cleanup) address of g_pA[1]: 0x5556ef98d678
  305. (cleanup) value of g_pA[1]: 0x5556ef98d6c0
  306. (cleanup) free g_pA[1]
  307. (cleanup) address of g_pA[2]: 0x5556ef98d680
  308. (cleanup) value of g_pA[2]: 0x5556ef98d720
  309. (cleanup) free g_pA[2]
  310.  
  311. valgrind:
  312. HEAP SUMMARY:
  313. in use at exit: 0 bytes in 0 blocks
  314. total heap usage: 7 allocs, 7 frees, 1,312 bytes allocated
  315. All heap blocks were freed -- no leaks are possible
  316. ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
  317.  
  318. */
Advertisement
Add Comment
Please, Sign In to add comment