Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- // alokace pole na haldě
- int* heapArray = (int*) malloc(sizeof(*heapArray)); // nebo v C++ int* heapArray = new int;
- heapArray[0] = 0; // *(heapArray) = 0;
- heapArray[1] = 1; // *(heapArray + 1) = 1;
- heapArray[2] = 2; // *(heapArray + 2) = 2;
- //v pořádku
- printf("%d ", heapArray[0]);
- printf("%d ", heapArray[1]);
- printf("%d \n", heapArray[2]);
- printf("%p ", &heapArray[0]);
- printf("%p ", &heapArray[1]);
- printf("%p \n", &heapArray[2]);
- //jakou hodnotu má dalších 7 políček a existují vůbec?
- heapArray[10] = 500;
- //pointerová aritmetika funguje
- printf("%d ", *(heapArray+10));
- printf("\n");
- /*pokud funguje kód výše proč bych vůbec při alokaci pole měl udávat počet políček?
- int* heapArray = (int*) malloc(pocet_policek * sizeof(*heapArray)); ? */
- //*******************************************************************************//
- int* heapArray2 = (int*) malloc(sizeof(*heapArray2));
- int velikost_pole = 0;
- printf("zadej pocatecni velikost pole: ");
- scanf("%d", &velikost_pole);
- //počáteční velikost
- for(int x = 0; x < velikost_pole; x++){
- heapArray2[x] = x;
- }
- for(int x = 0; x < velikost_pole; x++){
- printf("%d ", heapArray2[x]);
- }
- printf("\n");
- //rozšíření pole
- for(int x = velikost_pole; x < velikost_pole*2; x++){
- heapArray2[x] = x;
- }
- for(int x = velikost_pole; x < velikost_pole*2; x++){
- printf("%d ", heapArray2[x]);
- }
- printf("\n");
- //vypsání celého pole
- for(int x = 0; x < velikost_pole*2; x++){
- printf("%d ", heapArray2[x]);
- }
- free(heapArray2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement