Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Learning pointers to arrays
- *
- * -std=c99 -Wall -Wextra -Wpedantic -Wshadow
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- int** g_pA = NULL;
- unsigned int g_uiE = 0;
- void cleanup()
- {
- if(g_uiE)
- {
- for(unsigned int i = 0; i < g_uiE; ++i)
- {
- printf("(cleanup) address of g_pA[%d]: %p\n", i, (void*) &g_pA[i]);
- printf("(cleanup) value of g_pA[%d]: %p\n", i, (void*) g_pA[i]);
- printf("(cleanup) free g_pA[%d]\n",i);
- free(g_pA[i]);
- }
- }
- if(g_pA) free(g_pA);
- }
- bool init(int*** a, unsigned int* e)
- {
- int** arrTmp = NULL;
- printf("(init 1) a: %p\n", (void*)a);
- printf("(init 2) *e: %d\n",*e);
- *a = realloc(*a, sizeof(int*)*(*e+1));
- if(!*a) return false;
- (*a)[*e] = malloc(sizeof(int)*10);
- if(!(*a)[*e]) return false;
- printf("(init 3) *a: %p\n", (void*)*a);
- printf("(init 4) (*a)[*e]: %p\n", (void*)(*a)[*e]);
- (*e)++;
- printf("(init 5) *e: %d\n",*e);
- arrTmp = realloc(*a, sizeof(int*)*(*e+1));
- if(!arrTmp) return false;
- *a = arrTmp;
- (*a)[*e] = malloc(sizeof(int)*20);
- if(!(*a)[*e]) return false;
- printf("(init 6) *a: %p\n", (void*)*a);
- printf("(init 7) (*a)[*e]: %p\n", (void*)(*a)[*e]);
- (*e)++;
- printf("(init 8) *e: %d\n",*e);
- arrTmp = realloc(*a, sizeof(int*)*(*e+1));
- if(!arrTmp) return false;
- *a = arrTmp;
- (*a)[*e] = malloc(sizeof(int)*30);
- if(!(*a)[*e]) return false;
- printf("(init 9) *a: %p\n", (void*)*a);
- printf("(init 10) (*a)[*e]: %p\n", (void*)(*a)[*e]);
- (*e)++;
- printf("(init 11) *e: %d\n",*e);
- return true;
- }
- int main (int argc, char **argv){
- int z[10] = {0};
- int* ip;
- ip=&z[0];
- *ip=1;
- printf("sizeof int %zu\n",sizeof(int));
- printf("sizeof int* %zu\n\n",sizeof(int*));
- {
- int* arr = z;
- int* tmp;
- tmp = arr;
- printf("(1) address of tmp: %p\n", (void*) tmp);
- printf("(2) address of tmp[0]: %p\n", (void*) &tmp[0]);
- //tmp[i] = x;
- tmp += 1; //adds sizeof(int) to address
- printf("(3) address of tmp: %p\n", (void*) tmp);
- *tmp = 2;
- tmp++; //adds sizeof(int) to address
- printf("(4) address of tmp: %p\n", (void*) tmp);
- *tmp = 3;
- // address of tmp is incremented two times by sizeof(int)
- printf("(5) address of tmp[0]: %p\n", (void*) &tmp[0]);
- printf("(6) address of tmp[1]: %p\n", (void*) &tmp[1]);
- printf("(7) address of tmp[2]: %p\n\n", (void*) &tmp[2]);
- tmp[2] = 5;
- }
- printf("%d\n",z[0]);
- printf("%d\n",z[1]);
- printf("%d\n",z[2]);
- printf("%d\n",z[3]);
- printf("%d\n\n",z[4]);
- {
- unsigned int el[] = {10,20};
- unsigned int* pel;
- unsigned int *a, *b;
- pel = el;
- //notes:
- //wrong: (void*)pel+1 warning: pointer of type ‘void *’ used in arithmetic, and plus 1 byte
- //(void*)(pel+1) pel is (unsigned int*), plus sizeof(unsigned int)
- printf("(pel)(1) address pel: %p\n",(void*)pel);
- printf("(pel)(1) address pel+1: %p\n",(void*)(pel+1)); //plus sizeof(unsigned int)
- printf("(pel)(1) the difference (in bytes): %ld\n\n",((pel+1)-pel)*sizeof(unsigned int));
- printf("(pel)(2) address pel: %p\n",(void*)pel);
- printf("(pel)(2) address pel++: %p\n",(void*)pel++); //postfix
- printf("(pel)(2) address pel: %p\n\n",(void*)pel);
- pel = el;
- printf("(pel)(3) address ++pel: %p\n",(void*)++pel);
- printf("(pel)(3) address pel: %p\n\n",(void*)pel);
- pel = el;
- printf("(pel)(4) address &pel[0]: %p\n",(void*)&pel[0]);
- printf("(pel)(4) address &pel[1]: %p\n",(void*)&pel[1]);
- printf("(pel)(4) the difference (in bytes): %ld\n\n",(&pel[1]-&pel[0])*sizeof(unsigned int));
- a = pel;
- b = pel+1;
- printf("(pel)(5) address a: %p\n",(void*)a);
- printf("(pel)(5) address b: %p\n",(void*)b);
- printf("(pel)(5) the difference (in bytes): %ld\n\n",(b-a)*sizeof(unsigned int));
- printf("(pel)(6) address pel: %p\n",(void*)pel);
- printf("(pel)(6) pel[0]: %d\n",pel[0]);
- printf("(pel)(6) *pel: %d\n",*pel);
- printf("(pel)(6) (*pel)++: %d\n",(*pel)++);
- printf("(pel)(6) *pel: %d\n",*pel);
- printf("(pel)(6) *pel++: %d\n",*pel++); //postfix
- printf("(pel)(6) *pel: %d\n\n",*pel);
- printf("(pel)(7) *el: %d\n",*el);
- printf("(pel)(7) *el+1: %d\n",*el+1);
- printf("(pel)(7) (*el)+1: %d\n",(*el)+1);
- printf("(pel)(7) *(el+1): %d\n\n",*(el+1));
- }
- if(!init(&g_pA,&g_uiE))
- {
- cleanup();
- return 1;
- }
- printf("init OK. !\n");
- printf("g_uiE: %d\n",g_uiE);
- //just testing
- memset(g_pA[0], 0, 10*sizeof(unsigned int));
- memset(g_pA[1], 0, 20*sizeof(unsigned int));
- memset(g_pA[2], 0, 30*sizeof(unsigned int));
- g_pA[0][3] = 123;
- printf("%d\n",g_pA[0][3]);
- cleanup();
- return 0;
- }
- /*
- output:
- sizeof int 4
- sizeof int* 8
- (1) address of tmp: 0x7ffd942690b0
- (2) address of tmp[0]: 0x7ffd942690b0
- (3) address of tmp: 0x7ffd942690b4
- (4) address of tmp: 0x7ffd942690b8
- (5) address of tmp[0]: 0x7ffd942690b8
- (6) address of tmp[1]: 0x7ffd942690bc
- (7) address of tmp[2]: 0x7ffd942690c0
- 1
- 2
- 3
- 0
- 5
- (pel)(1) address pel: 0x7ffd942690a8
- (pel)(1) address pel+1: 0x7ffd942690ac
- (pel)(1) the difference (in bytes): 4
- (pel)(2) address pel: 0x7ffd942690a8
- (pel)(2) address pel++: 0x7ffd942690a8
- (pel)(2) address pel: 0x7ffd942690ac
- (pel)(3) address ++pel: 0x7ffd942690ac
- (pel)(3) address pel: 0x7ffd942690ac
- (pel)(4) address &pel[0]: 0x7ffd942690a8
- (pel)(4) address &pel[1]: 0x7ffd942690ac
- (pel)(4) the difference (in bytes): 4
- (pel)(5) address a: 0x7ffd942690a8
- (pel)(5) address b: 0x7ffd942690ac
- (pel)(5) the difference (in bytes): 4
- (pel)(6) address pel: 0x7ffd942690a8
- (pel)(6) pel[0]: 10
- (pel)(6) *pel: 10
- (pel)(6) (*pel)++: 10
- (pel)(6) *pel: 11
- (pel)(6) *pel++: 11
- (pel)(6) *pel: 20
- (pel)(7) *el: 11
- (pel)(7) *el+1: 12
- (pel)(7) (*el)+1: 12
- (pel)(7) *(el+1): 20
- (init 1) a: 0x5556eeb60018
- (init 2) *e: 0
- (init 3) *a: 0x5556ef98d670
- (init 4) (*a)[*e]: 0x5556ef98d690
- (init 5) *e: 1
- (init 6) *a: 0x5556ef98d670
- (init 7) (*a)[*e]: 0x5556ef98d6c0
- (init 8) *e: 2
- (init 9) *a: 0x5556ef98d670
- (init 10) (*a)[*e]: 0x5556ef98d720
- (init 11) *e: 3
- init OK. !
- g_uiE: 3
- 123
- (cleanup) address of g_pA[0]: 0x5556ef98d670
- (cleanup) value of g_pA[0]: 0x5556ef98d690
- (cleanup) free g_pA[0]
- (cleanup) address of g_pA[1]: 0x5556ef98d678
- (cleanup) value of g_pA[1]: 0x5556ef98d6c0
- (cleanup) free g_pA[1]
- (cleanup) address of g_pA[2]: 0x5556ef98d680
- (cleanup) value of g_pA[2]: 0x5556ef98d720
- (cleanup) free g_pA[2]
- valgrind:
- HEAP SUMMARY:
- in use at exit: 0 bytes in 0 blocks
- total heap usage: 7 allocs, 7 frees, 1,312 bytes allocated
- All heap blocks were freed -- no leaks are possible
- ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
- */
Advertisement
Add Comment
Please, Sign In to add comment