RicardasSim

c pointers

Dec 24th, 2018
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.86 KB | None | 0 0
  1. //public domain
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. /*
  8.  
  9. (1) int var;
  10. (2) int var = 123;
  11. (3) &var
  12. (4) int* pnt;
  13. (4) *pnt
  14. (5) pnt = &var;
  15. (6) *pnt = var;
  16. (7) char str[] = "Zeppelin";
  17. (8) char arr[10]={0}; arr[0] = 'Z'; arr[1] = 'e';
  18. (9) char str[] = "Zeppelin"; address of str &str &str[0]
  19. (10) str[0]; str[1];
  20. (11) char str[] = "Zeppelin"; *str;
  21. (12) char str[] = "Zeppelin"; char* pnt;
  22. (13) char str[] = "Zeppelin"; *str; *(str+1); *(str+2);
  23. (14) char arr[3][3]={{'Z','e','p'},{'p','e','l'},{'i','n','\0'}};
  24. (15) char** arr;
  25. (16) char** arr; with realloc
  26. (17) char (*buff)[80];
  27. (18) char** int** double** sizeof
  28. (19) char* int* float* double* sizeof
  29. (20) function pointer
  30. (21) struct, st.a
  31. (22) typedef struct, st.a
  32. (23) struct stru* pSt; pSt=&st; pSt->a
  33. (24) struct stru* st; sizeof structure, malloc
  34. (25) various struct pointers and sizeof structure
  35. (26) n[arr]
  36. (27) pnt++; pnt--;
  37. (28) typedef name char[MAX];
  38. (29) malloc (sizeof(arr))
  39. (30) array size, address
  40. (31) pnt++; while
  41. (32) swap(char** a, char** b) swap(&strArr[0], &strArr[1]);
  42. (33) int arr[]={1,2,3}; int *ptr; ptr = &arr[0]; *(ptr) *(ptr+1)
  43. (34) char *pnt; malloc(); pnt[i];
  44. (35) int *pnt; malloc(); pnt[i];
  45.  
  46. */
  47.  
  48. void testfncA()
  49. {
  50.     printf("Zeppelin\n");
  51. }
  52.  
  53. void testfncB(char c)
  54. {
  55.     printf("%c\n", c);
  56. }
  57.  
  58. void testfncC(int x, int y)
  59. {
  60.     printf("%d %d\n", x, y);
  61. }
  62.  
  63. int testfncD(int x, int y)
  64. {
  65.     return x + y;
  66. }
  67.  
  68. void testfncE(int* a)
  69. {
  70.     *a+=1;
  71. }
  72.  
  73. void swap(char** a, char** b)
  74. {
  75.     char* tmp;
  76.    
  77.     tmp = *a;
  78.     *a = *b;
  79.     *b = tmp;  
  80. }
  81.  
  82. int main (int argc, char **argv){
  83.  
  84. //-----(1)-----
  85. {  
  86.     int var;
  87.    
  88.     var = 123;
  89.    
  90.     printf("(1) value of var = %d\n", var);
  91. }
  92.  
  93. //-----(2)-----
  94. {  
  95.     int var = 123;
  96.    
  97.     printf("(2) value of var = %d\n", var);
  98. }
  99.  
  100. //-----(3)-----    
  101. {  
  102.     int var = 123;
  103.    
  104.     printf("(3) value of var = %d\n", var);
  105.     printf("(3) address of var = %p\n", (void*) &var);
  106. }
  107.  
  108. //-----(4)-----    
  109. {  
  110.     int* pnt;
  111.    
  112.     pnt = malloc(sizeof *pnt);
  113.    
  114.     if(pnt)
  115.     {
  116.    
  117.         *pnt = 123;
  118.    
  119.         printf("(4) value of pnt = %d\n", *pnt);
  120.         printf("(4) address of pnt = %p\n", (void*) pnt);
  121.    
  122.         free(pnt);
  123.     }
  124. }
  125.  
  126. //-----(5)-----
  127. {
  128.     int var = 123;
  129.    
  130.     int* pnt;
  131.    
  132.     pnt = &var;
  133.    
  134.     printf("(5) value of pnt = %d\n", *pnt);
  135.     printf("(5) address of pnt = %p\n", (void*) pnt);  
  136.    
  137. }
  138.  
  139. //-----(6)-----
  140. {
  141.     int var = 123;
  142.    
  143.     int* pnt;
  144.    
  145.     pnt = malloc(sizeof(*pnt));
  146.  
  147.     if(pnt)
  148.     {    
  149.    
  150.         *pnt = var;
  151.    
  152.         printf("(6) value of pnt = %d\n", *pnt);
  153.         printf("(6) address of pnt = %p\n", (void*) pnt);
  154.        
  155.         free(pnt);
  156.     }    
  157. }
  158.  
  159. //-----(7)-----
  160. {
  161.     char str[] = "Zeppelin";
  162.    
  163.     printf("(7) %s\n", str);
  164. }
  165.  
  166. //-----(8)-----
  167. {
  168.     char arr[9]={0};
  169.    
  170.     arr[0] = 'Z';
  171.     arr[1] = 'e';
  172.     arr[2] = 'p';
  173.     arr[3] = 'p';
  174.     arr[4] = 'e';
  175.     arr[5] = 'l';
  176.     arr[6] = 'i';
  177.     arr[7] = 'n';
  178.    
  179.     //null terminated, because arr[8] is 0
  180.     printf("(8) %s\n", arr);
  181.  
  182. }
  183.  
  184. //-----(9)-----
  185. {
  186.     char str[] = "Zeppelin";
  187.    
  188.     printf("(9) address: %p\n", (void*) str);       //address of array
  189.     printf("(9) address: %p\n", (void*) &str);
  190.     printf("(9) address: %p\n", (void*) &str[0]);   //address of first element of an array
  191.     printf("(9) address: %p\n", (void*) &str[1]);   //address of second element of an array
  192.  
  193. }
  194.  
  195. //-----(10)-----
  196. {
  197.     char str[] = "Zeppelin";
  198.    
  199.     printf("(10) %c\n", str[0]);
  200.     printf("(10) %c\n", str[1]);
  201.  
  202. }
  203.  
  204. //-----(11)-----
  205. {
  206.     char str[] = "Zeppelin";
  207.    
  208.     printf("(11) %c\n", *str);
  209.     printf("(11) %c\n", str[0]);
  210.     printf("(11) %c\n", str[1]);
  211.  
  212. }
  213.  
  214. //-----(12)-----
  215. {
  216.     char str[] = "Zeppelin";
  217.    
  218.     char* pnt;
  219.    
  220.     pnt = str;
  221.    
  222.     printf("(12) %s\n", pnt);
  223.  
  224. }
  225.  
  226. //-----(13)-----
  227. {
  228.     char str[] = "Zeppelin";
  229.    
  230.     printf("(13) %c %c %c %c %c %c %c %c\n", *str, *(str+1), *(str+2), *(str+3), *(str+4), *(str+5), *(str+6), *(str+7));
  231.  
  232. }
  233.  
  234. //-----(14)-----
  235. {
  236.     char arr[3][3]={{'Z','e','p'},{'p','e','l'},{'i','n','\0'}};
  237.    
  238.     printf("(14) %c%c%c%c%c%c%c%c\n", arr[0][0], arr[0][1], arr[0][2], arr[1][0], arr[1][1], arr[1][2], arr[2][0], arr[2][1]);
  239. }
  240.  
  241.  
  242. //-----(15)-----
  243. {
  244.     char** arr;
  245.  
  246.     char str1[]="Zepp";
  247.     char str2[]="elin";
  248.  
  249.     arr = (char**) malloc(sizeof *arr *2);
  250.            
  251.     if(arr)
  252.     {
  253.  
  254.         arr[0] = malloc(strlen(str1)+1);
  255.        
  256.         if(arr[0])
  257.         {
  258.            
  259.             //memset(arr[0] + strlen(str1), 0, 1);
  260.            
  261.             arr[1] = malloc(strlen(str2)+1);
  262.            
  263.             if(arr[1])
  264.             {
  265.                
  266.                 //memset(arr[1] + strlen(str2), 0, 1);
  267.                
  268.                 //strcpy() function copies the string, including the terminating null byte.
  269.                 strcpy(arr[0],str1);
  270.                 strcpy(arr[1],str2);
  271.                
  272.                 printf("(15) %c%c%c%c%c%c%c%c\n", arr[0][0], arr[0][1], arr[0][2], arr[0][3], arr[1][0], arr[1][1], arr[1][2], arr[1][3]);
  273.                              
  274.                 printf("(15) %s%s\n",arr[0],arr[1]);                
  275.                
  276.                 free(arr[1]);
  277.             }
  278.            
  279.             free(arr[0]);
  280.         }
  281.        
  282.         free(arr);
  283.     }
  284.  
  285. }
  286.  
  287. //-----(16)-----
  288. {
  289.  
  290.     char** arr = NULL;
  291.    
  292.     char** arr_tmp;
  293.    
  294.     char str1[]="first";
  295.     char str2[]="second";  
  296.     char str3[]="third";
  297.    
  298.     int cnt = 0;
  299.    
  300.     /*
  301.      void *realloc(void *ptr, size_t size);
  302.      if ptr is a null pointer, realloc() behaves like malloc()
  303.     */
  304.     arr = (char**) realloc(NULL,sizeof *arr *(cnt+1));
  305.    
  306.     if(!arr)
  307.     {
  308.         printf("ERROR! arr (%d) memory not allocated.\n",cnt);
  309.         goto lbl_exit;
  310.     }
  311.  
  312.     arr[cnt] = malloc(strlen(str1)+1);
  313.    
  314.     if(!arr[cnt])
  315.     {
  316.         printf("ERROR! arr[%d] memory not allocated.\n",cnt);
  317.         goto lbl_exit;
  318.     }
  319.    
  320.     //strcpy() function copies the string, including the terminating null byte.
  321.     strcpy(arr[cnt],str1);
  322.    
  323.     cnt++;
  324.    
  325.  
  326.     arr_tmp = (char**) realloc(arr,sizeof *arr *(cnt+1));
  327.    
  328.     if(!arr_tmp)
  329.     {
  330.         printf("ERROR! arr (%d) memory not allocated.\n",cnt);
  331.         goto lbl_exit;
  332.     }    
  333.      
  334.     arr = arr_tmp;
  335.      
  336.     arr[cnt] = malloc(strlen(str2)+1);
  337.    
  338.     if(!arr[cnt])
  339.     {
  340.         printf("ERROR! arr[%d] memory not allocated.\n",cnt);
  341.         goto lbl_exit;
  342.     }
  343.    
  344.     //strcpy() function copies the string, including the terminating null byte.
  345.     strcpy(arr[cnt],str2);
  346.  
  347.     cnt++;
  348.    
  349.  
  350.     arr_tmp = (char**) realloc(arr,sizeof *arr *(cnt+1));
  351.    
  352.     if(!arr_tmp)
  353.     {
  354.         printf("ERROR! arr (%d) memory not allocated.\n",cnt);
  355.         goto lbl_exit;
  356.     }    
  357.    
  358.     arr = arr_tmp;
  359.    
  360.     arr[cnt] = malloc(strlen(str3)+1);
  361.    
  362.     if(!arr[cnt])
  363.     {
  364.         printf("ERROR! arr[%d] memory not allocated.\n",cnt);
  365.         goto lbl_exit;
  366.     }
  367.    
  368.     //strcpy() function copies the string, including the terminating null byte.
  369.     strcpy(arr[cnt],str3);
  370.    
  371.     cnt++;
  372.    
  373.    
  374.     printf("(16) %s %s %s\n", arr[0], arr[1], arr[2]);
  375.    
  376.     lbl_exit: ;
  377.    
  378.     if(arr){
  379.          
  380.             for(int i = 0;i<cnt;i++)
  381.             {
  382.                
  383.                 if(arr[i]) free(arr[i]);
  384.                
  385.             }
  386.            
  387.            
  388.             free(arr);
  389.         }
  390. }
  391.  
  392. //-----(17)-----
  393. {
  394.    
  395.     char (*buff)[80];
  396.    
  397.     buff = malloc(sizeof *buff *3);
  398.    
  399.     if(buff)
  400.     {
  401.    
  402.         buff[2][3] = 'C';
  403.        
  404.         printf("(17) %c\n", buff[2][3]);
  405.        
  406.         free(buff);
  407.     }
  408.    
  409. }
  410.  
  411. //-----(18)-----
  412. {
  413.  
  414.     char** pArr1;
  415.     int** pArr2;
  416.     double** pArr3;
  417.  
  418.     printf("(18) %zu\n", sizeof *pArr1);
  419.     printf("(18) %zu\n", sizeof *pArr2);
  420.     printf("(18) %zu\n", sizeof *pArr3);
  421.  
  422. }
  423.  
  424. //-----(19)-----
  425. {
  426.  
  427.     char* cpnt;
  428.     int* ipnt;
  429.     float* fpnt;
  430.     double* dpnt;
  431.  
  432.     printf("(19) %zu\n", sizeof *cpnt);
  433.     printf("(19) %zu\n", sizeof *ipnt);
  434.     printf("(19) %zu\n", sizeof *fpnt);
  435.     printf("(19) %zu\n", sizeof *dpnt);
  436.  
  437. }
  438.  
  439. //-----(20)-----
  440. {
  441.  
  442.     printf("(20) ");
  443.    
  444.    
  445.     void(*fncPtrA)() = &testfncA;
  446.  
  447.     (*fncPtrA)();
  448.    
  449.    
  450.     printf("(20) ");
  451.    
  452.    
  453.     void(*fncPtrB)(char) = &testfncB;
  454.  
  455.     (*fncPtrB)('C');
  456.    
  457.    
  458.     printf("(20) ");
  459.    
  460.    
  461.     void(*fncPtrC)(int, int) = &testfncC;
  462.  
  463.     (*fncPtrC)(1,2);
  464.  
  465.  
  466.     int sum;
  467.    
  468.     int(*fncPtrD)(int, int) = &testfncD;
  469.  
  470.     sum = (*fncPtrD)(1,2);
  471.  
  472.     printf("(20) %d\n", sum);
  473.  
  474.    
  475.     int val = 1;
  476.    
  477.     void(*fncPtrE)(int*) = &testfncE;
  478.    
  479.     (*fncPtrE)(&val);
  480.    
  481.     printf("(20) %d\n", val);
  482.    
  483. }
  484.  
  485. //-----(21)-----
  486. {
  487.     struct stru {
  488.       int a;  
  489.       int b;  
  490.       char c;
  491.       float f;
  492.       double d;
  493.     };
  494.    
  495.     struct stru st;
  496.    
  497.     st.a = 1;
  498.     st.b = 2;
  499.     st.c = 'C';
  500.     st.f = 0.5f;
  501.     st.d = 1.2;
  502.    
  503.     printf("(21) %d %d %c %f %f\n", st.a, st.b, st.c, st.f, st.d);
  504.    
  505. }
  506.  
  507. //-----(22)-----
  508. {
  509.     typedef struct stru {
  510.       int a;  
  511.       int b;  
  512.       char c;
  513.       float f;
  514.       double d;
  515.     }Stru;
  516.    
  517.     Stru st;
  518.    
  519.     st.a = 1;
  520.     st.b = 2;
  521.     st.c = 'C';
  522.     st.f = 0.5f;
  523.     st.d = 1.2;
  524.    
  525.     printf("(22) %d %d %c %f %f\n", st.a, st.b, st.c, st.f, st.d);
  526.    
  527. }
  528.  
  529. //-----(23)-----
  530. {
  531.     struct stru {
  532.       int a;  
  533.       int b;  
  534.       char c;
  535.       float f;
  536.       double d;
  537.     };
  538.    
  539.     struct stru st;
  540.        
  541.     struct stru* pSt;
  542.    
  543.     pSt=&st;
  544.    
  545.     pSt->a = 1;
  546.     pSt->b = 2;
  547.     pSt->c = 'C';
  548.     pSt->f = 0.5f;
  549.     pSt->d = 1.2;
  550.    
  551.     printf("(23) %d %d %c %f %f\n", st.a, st.b, st.c, st.f, st.d);
  552.    
  553. }
  554.  
  555. //-----(24)-----
  556. {
  557.     struct stru {
  558.       int a;  
  559.       int b;  
  560.       char c;
  561.       float f;
  562.       double d;
  563.     };
  564.    
  565.     struct stru* st;
  566.    
  567.     printf("(24) sizeof structure %zu\n", sizeof (struct stru));
  568.     printf("(24) sizeof structure %zu\n", sizeof *st);
  569.    
  570.     st = malloc(sizeof (struct stru));
  571.     if(st)
  572.     {
  573.        
  574.         st->a = 1;
  575.         st->b = 2;
  576.         st->c = 'C';
  577.         st->f = 0.5f;
  578.         st->d = 1.2;      
  579.        
  580.         printf("(24) %d %d %c %f %f\n", st->a, st->b, st->c, st->f, st->d);
  581.        
  582.         free(st);
  583.     }
  584. }
  585.  
  586.  
  587. //-----(25)-----
  588. {
  589.     typedef struct stru {
  590.       int a;  
  591.       int b;  
  592.       char c;
  593.       float f;
  594.       double d;
  595.     }Stru;
  596.    
  597.     struct stru st;
  598.    
  599.     struct stru* pst;
  600.    
  601.     Stru stt;
  602.    
  603.     Stru* pStt;
  604.    
  605.     printf("(25) sizeof structure %zu\n", sizeof (struct stru));
  606.     printf("(25) sizeof structure %zu\n", sizeof st);
  607.     printf("(25) sizeof structure %zu\n", sizeof *pst);
  608.     printf("(25) sizeof structure %zu\n", sizeof stt);
  609.     printf("(25) sizeof structure %zu\n", sizeof *pStt);
  610.  
  611. }
  612.  
  613. //-----(26)-----
  614. {
  615.     char arr[]={"Zeppelin"};
  616.    
  617.     int len;
  618.    
  619.     printf("(26) ");
  620.    
  621.     len = strlen(arr);
  622.    
  623.     for(int i = 0; i<len; i++){
  624.        
  625.         putchar(i[arr]);
  626.        
  627.     }
  628.     putchar(0x0A);
  629.  
  630. }
  631.  
  632. //-----(27)-----
  633. {
  634.     char str[]={"abcdefghij"};
  635.    
  636.     int len, i;
  637.    
  638.     char* pnt;
  639.    
  640.     pnt = str;
  641.    
  642.     printf("(27) ");
  643.    
  644.     len = strlen(str);
  645.    
  646.     for(i = 0; i<len; i++){
  647.        
  648.         putchar(*pnt);
  649.         pnt++;
  650.     }
  651.  
  652.     printf("\n(27) ");
  653.    
  654.     pnt = str + len - 1;
  655.    
  656.     for(i = 0; i<len; i++){
  657.        
  658.         putchar(*pnt);
  659.         pnt--;
  660.     }
  661.    
  662.    
  663.     char arr[]={'a','b','c','d','e','f','g','h','i','j'};
  664.    
  665.     int num_of_e = sizeof(arr); //sizeof(char) is 1, sizeof(arr)/sizeof(arr[0]) unnecessary
  666.    
  667.     printf("\n(27) ");
  668.    
  669.     pnt = arr;
  670.    
  671.     for(i = 0; i<num_of_e; i++){
  672.        
  673.         putchar(*pnt);
  674.         pnt++;
  675.     }  
  676.    
  677.     printf("\n(27) ");
  678.    
  679.     pnt = arr + num_of_e - 1;
  680.    
  681.     for(i = 0; i<num_of_e; i++){
  682.        
  683.         putchar(*pnt);
  684.         pnt--;
  685.     }    
  686.    
  687.     putchar(0x0A);
  688.    
  689. }
  690.  
  691. //-----(28)-----
  692. {
  693.    
  694.     #define MAX_MESSAGE_LENGHT 64
  695.    
  696.     typedef char Message[MAX_MESSAGE_LENGHT];
  697.    
  698.     Message messages[3];
  699.    
  700.     strcpy( messages[0], "(28) first\n" );
  701.     strcpy( messages[1], "(28) second\n" );
  702.     strcpy( messages[2], "(28) third\n" );
  703.    
  704.     for(unsigned int i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
  705.     {
  706.         printf( "%s",messages[i] );
  707.     }
  708.    
  709. }
  710.  
  711. //-----(29)-----
  712. {
  713.     printf("(29) ");
  714.    
  715.     int arr[]={0,1,2,3,4,5,6,7,8,9};
  716.    
  717.     int* pnt1 = malloc(sizeof(arr));
  718.    
  719.     if(pnt1)
  720.     {
  721.  
  722.         memcpy(pnt1,arr,sizeof(arr));
  723.        
  724.         for(unsigned int i=0; i < sizeof(arr)/sizeof(arr[0]); i++)
  725.         {
  726.            
  727.             printf("%d ", arr[i] );
  728.        
  729.         }
  730.        
  731.         free(pnt1);
  732.     }
  733.     else printf("ERROR! memory not allocated.");
  734.    
  735.     printf("\n");
  736. }
  737.  
  738. //-----(30)-----
  739. {
  740.     char str[] = "Cassiopeia";
  741.    
  742.     printf("(30) sizeof str:\t%zu\taddress: %p\n", sizeof str, (void*) str);
  743.     printf("(30) sizeof &str:\t%zu\taddress: %p\n", sizeof &str, (void*) &str);
  744.     printf("(30) sizeof &str[0]:\t%zu\taddress: %p\n", sizeof &str[0], (void*) &str[0]);
  745.     printf("(30) sizeof &str[1]:\t%zu\taddress: %p\n", sizeof &str[1], (void*) &str[1]);
  746.     printf("(30) sizeof &str[2]:\t%zu\taddress: %p\n", sizeof &str[2], (void*) &str[2]);
  747.  
  748.     int arr[] = {1,2,3,4,5,6,7,8,9,0,1};
  749.  
  750.     printf("(30) sizeof arr:\t%zu\taddress: %p\n", sizeof arr, (void*) arr);
  751.     printf("(30) sizeof &arr:\t%zu\taddress: %p\n", sizeof &arr, (void*) &arr);
  752.     printf("(30) sizeof &arr[0]:\t%zu\taddress: %p\n", sizeof &arr[0], (void*) &arr[0]);
  753.     printf("(30) sizeof &arr[1]:\t%zu\taddress: %p\n", sizeof &arr[1], (void*) &arr[1]);
  754.    
  755.     printf("(30) &arr[0] address: %p\n", (void*) &arr[0]);
  756.     printf("(30) &arr[1] address: %p\n", (void*) &arr[1]);
  757.     printf("(30) &arr[2] address: %p\n", (void*) &arr[2]);
  758.  
  759.     printf("(30) sizeof *arr:\t%zu\n", sizeof *arr);
  760.     printf("(30) sizeof arr[0]:\t%zu\n", sizeof arr[0]);
  761.  
  762. }
  763.  
  764. //-----(31)-----
  765. {
  766.     char arr[]={"Zeppelin"};
  767.    
  768.     char* pnt;
  769.    
  770.     int len = strlen(arr);
  771.    
  772.     pnt = arr;
  773.    
  774.     printf("(31) ");
  775.    
  776.     while( pnt < &arr[len] )
  777.     {
  778.         putchar(*pnt);
  779.         pnt++;
  780.     }
  781.    
  782.     putchar('\n');
  783.    
  784. }
  785.  
  786. //-----(32)-----
  787. {
  788.     char* strArr[]={"first","second"};
  789.    
  790.     printf("(32) %s %s\n",strArr[0], strArr[1]);
  791.    
  792.     swap(&strArr[0], &strArr[1]);
  793.    
  794.     printf("(32) %s %s\n",strArr[0], strArr[1]);
  795.    
  796. }
  797.  
  798. //-----(33)-----
  799. {
  800.    
  801.     int arr[]={1,2,3};
  802.    
  803.     int *ptr;
  804.    
  805.     ptr = &arr[0];
  806.    
  807.     printf("(33) %d ",*(ptr));
  808.     printf("%d ",*(ptr + 1));
  809.     printf("%d\n",*(ptr + 2));
  810.    
  811. }
  812.  
  813. //-----(34)-----
  814. {
  815.     char str[]={"Zeppelin"};
  816.    
  817.     char *pnt;
  818.  
  819.     int len = strlen(str);
  820.    
  821.     printf("(34) ");
  822.    
  823.     pnt = malloc(len+1);
  824.    
  825.     if(pnt)
  826.     {
  827.         strcpy(pnt, str);
  828.        
  829.         for(int i=0; i < len; i++)
  830.         {
  831.             putchar(pnt[i]);
  832.         }
  833.        
  834.         free(pnt);
  835.     }
  836.     else printf("ERROR! memory not allocated.");
  837.    
  838.     printf("\n");
  839. }
  840.  
  841. //-----(35)-----
  842. {
  843.  
  844.     int arr[]={0,1,2,3,4,5,6,7,8,9};
  845.    
  846.     int *pnt;
  847.  
  848.     int size = sizeof(arr);
  849.     int num_of_e = sizeof(arr)/sizeof(arr[0]);
  850.    
  851.     printf("(35) ");
  852.    
  853.     pnt = malloc(size);
  854.    
  855.     if(pnt)
  856.     {
  857.         memcpy(pnt, arr, size);
  858.        
  859.         for(int i=0; i < num_of_e; i++)
  860.         {
  861.             printf("%d ",pnt[i]);
  862.         }
  863.        
  864.         free(pnt);
  865.     }
  866.     else printf("ERROR! memory not allocated.");
  867.    
  868.     printf("\n");
  869.    
  870. }
  871.  
  872. return 0;
  873. }
Advertisement
Add Comment
Please, Sign In to add comment