Advertisement
GodlyPacketz

[C/Learn] Dynamic Memory/HEAP allocations

Oct 19th, 2023
1,291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. Operators -
  2.     - & refrence/address operator
  3.         - returns an address to some memory
  4.     - * derefrence/pointer operator
  5.         - returns the data at some address
  6.  
  7. int *name -
  8.     - name is variable name (not *name)
  9.     - name is type "Pointer to type int"
  10.     - the * in name makes name point to the address
  11.     of another integer variable
  12.  
  13. int var = 10; -------> var = 10
  14.  
  15. int *ptr = &var;
  16.     *ptr = 20; -----> var = 20
  17.  
  18. int **ptr2 = &ptr;
  19.     **ptr2 = 30; ----> var = 30
  20.  
  21. To access a refrenced variable use defrefrence operator
  22.  
  23. void example1() {
  24.     int var = 20;
  25.     int *ptr = &var;
  26.    
  27.     *ptr += 50;
  28.    
  29.     printf("Var's Value -> %d\r\n", *ptr);
  30.    
  31.     return;
  32. }
  33.  
  34. Changed value of var to 70 (20 + 50)
  35.  
  36. Memory:
  37.     - VAR[30]-0x03256
  38.     - PTR[0x03256]-0x043265
  39.  
  40. void example2() {
  41.     int var[] = {5, 7, 10};
  42.     int *ptr = var;
  43.  
  44.     for(i = 0; i < 3; i++) {
  45.         printf("Value of ptr -> %d\r\n", *ptr);
  46.         ptr++;
  47.     }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement