Advertisement
NealPeteros

Untitled

Apr 19th, 2023
1,468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 1.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main() {
  5.     int size1, size2;
  6.    
  7.     size1 = 7;
  8.     size2 = 10;
  9.    
  10.     printf("Address of size1: %d\n", &size1);
  11.     printf("Address of size2: %d\n", &size2);
  12.    
  13.     printf("\n\n");
  14.    
  15.     int *ptr;
  16.     ptr = &size1;
  17.     printf("ptr = %d\n", ptr);
  18.    
  19.     printf("\n\n");
  20.    
  21.     printf("Value of size1: %d\n", size1);
  22.     printf("Value of size2: %d\n", size2);
  23.     printf("Value of *ptr: %d\n", *ptr);
  24.    
  25.     printf("\n\n");
  26.    
  27.     // MALLOC
  28.    
  29.     /*
  30.         malloc(size);
  31.     */
  32.    
  33.     printf("sizeof(int): %d\n", sizeof(int));
  34.    
  35.     int *mal;
  36.     mal = malloc(sizeof(int) * 3); // mal is now 12 bytes
  37.    
  38.     printf("\n\n");
  39.    
  40.     int i;
  41.     for(i = 0; i < 3; i++) {
  42.         scanf("%d", mal+i);
  43.        
  44.         /*
  45.             i = 0;
  46.             mal+i = mal+0 = mal
  47.            
  48.             i = 1;
  49.             mal+i = mal+1
  50.         */
  51.     }
  52.  
  53.     printf("mal: %d\n", *mal);
  54.     printf("mal+1: %d\n", *(mal+1));
  55.     printf("mal+2: %d\n", *(mal+2));
  56.    
  57.     printf("\n\n");
  58.    
  59.     printf("mal address: %d\n", mal);
  60.     printf("mal+1 address: %d\n", mal+1);
  61.     printf("mal+2 address: %d\n", mal+2);
  62.    
  63.    
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement