Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. // alokace pole na haldě
  7. int* heapArray = (int*) malloc(sizeof(*heapArray)); // nebo v C++ int* heapArray = new int;
  8. heapArray[0] = 0; // *(heapArray) = 0;
  9. heapArray[1] = 1; // *(heapArray + 1) = 1;
  10. heapArray[2] = 2; // *(heapArray + 2) = 2;
  11.  
  12. //v pořádku
  13. printf("%d ", heapArray[0]);
  14. printf("%d ", heapArray[1]);
  15. printf("%d \n", heapArray[2]);
  16.  
  17. printf("%p ", &heapArray[0]);
  18. printf("%p ", &heapArray[1]);
  19. printf("%p \n", &heapArray[2]);
  20.  
  21. //jakou hodnotu má dalších 7 políček a existují vůbec?
  22. heapArray[10] = 500;
  23.  
  24. //pointerová aritmetika funguje
  25. printf("%d ", *(heapArray+10));
  26. printf("\n");
  27.  
  28. /*pokud funguje kód výše proč bych vůbec při alokaci pole měl udávat počet políček?
  29. int* heapArray = (int*) malloc(pocet_policek * sizeof(*heapArray)); ? */
  30.  
  31. //*******************************************************************************//
  32.  
  33. int* heapArray2 = (int*) malloc(sizeof(*heapArray2));
  34. int velikost_pole = 0;
  35.  
  36. printf("zadej pocatecni velikost pole: ");
  37. scanf("%d", &velikost_pole);
  38.  
  39. //počáteční velikost
  40. for(int x = 0; x < velikost_pole; x++){
  41. heapArray2[x] = x;
  42. }
  43.  
  44. for(int x = 0; x < velikost_pole; x++){
  45. printf("%d ", heapArray2[x]);
  46. }
  47.  
  48. printf("\n");
  49.  
  50. //rozšíření pole
  51. for(int x = velikost_pole; x < velikost_pole*2; x++){
  52. heapArray2[x] = x;
  53. }
  54.  
  55. for(int x = velikost_pole; x < velikost_pole*2; x++){
  56. printf("%d ", heapArray2[x]);
  57. }
  58.  
  59. printf("\n");
  60.  
  61. //vypsání celého pole
  62. for(int x = 0; x < velikost_pole*2; x++){
  63. printf("%d ", heapArray2[x]);
  64. }
  65.  
  66. free(heapArray2);
  67.  
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement