Advertisement
gigabytemon

C Examples: Pointers

Jul 6th, 2020
1,572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.02 KB | None | 0 0
  1. // FILE I/O EXAMPLE - C
  2. // (c) gigabytemon
  3. // 04/07/2020
  4. // 2862 abc5 66cb a008 dbd9 5bf7 0dda 04f8
  5. // e192 33e4 e689 4efb 24b9 d34f 6c2d 1ee1
  6.  
  7. /* introduction
  8.  * ------------
  9.  * every variable declaration is a location in your computer's memory.
  10.  * that location has an address, which is a 8-character long hexadecimal number.
  11.  * for example:
  12.  * int i; <-- this 'i' is a location in memory where an integer is stored
  13.  * so 'i' is a symbolic representation of that location in memory.
  14.  * meanwhile, its proper address could be, say, 0xbff5a400
  15.  *
  16.  * don't worry about the hexadecimal. just understand that it's an address.
  17.  * much like a house address. "gigabytemon's house" (the variable) and "the address of gigabytemon's house" (the address) both lead to the same thing.
  18.  * the only difference is that pointers give you the address of the variable, rather than the value stored inside the variable.
  19.  */
  20.  
  21.  /* syntax
  22.  * ------
  23.  * the general syntax of a pointer is:
  24.  *
  25.  * type *pointer_name
  26.  * pointer_name = &variable
  27.  *
  28.  * like any variable, the pointer must be declared with a type and a name.
  29.  * the '*' tells the compiler that this declaration is for a pointer.
  30.  * the pointer's type must be the same as the variable you are associating it with
  31.  */
  32.  
  33. /* personal notes
  34.  * --------------
  35.  * pointers are very useful for memory management and manipulation.
  36.  * they can store and manage dynamically allocated blocks of memory.
  37.  *
  38.  * i hate them.
  39.  */
  40.  
  41.  #include <stdio.h>
  42.  
  43.  void main()
  44.  {
  45.     //------------------------------------------------------------------------------------------------
  46.     // >> 1) Assigning pointers to variables
  47.     //------------------------------------------------------------------------------------------------
  48.     // variable declaration
  49.     int number = 8;         // lets assume this variable is stored at address 0x1000
  50.    
  51.     // the pointer we'll use to point to 'number'. currently set to NULL
  52.     int *numberPtr = NULL;      // assume this pointer is stored right after number, i.e. 0x1004
  53.    
  54.     //      memory table
  55.     /*/----------------------\
  56.      *| Address |  Contents  |
  57.      *|----------------------|
  58.      *|  0x1000 | 0x00000008 |  <--- this is where number = 8 is stored
  59.      *|  0x1004 | 0x00000000 |  <--- this is the where the pointer is stored (currently set to NULL)
  60.      *\----------------------/
  61.      */
  62.    
  63.     // now we'll assign the pointer to the 'number' variable
  64.     numberPtr = &number;        // 'number' pointer = address of 'number'
  65.    
  66.     //      memory table
  67.     /*/----------------------\
  68.      *| Address |  Contents  |
  69.      *|----------------------|
  70.      *|  0x1000 | 0x00000008 |  <--- number
  71.      *|  0x1004 | 0x00001000 |  <--- now the pointer's value is set to 'number''s address (0x1000)
  72.      *\----------------------/
  73.      */
  74.      
  75.     //------------------------------------------------------------------------------------------------
  76.     // >> 2) Retrieving information related to the pointer
  77.     //------------------------------------------------------------------------------------------------
  78.     // to get the address stored in the pointer numberPtr, do this:
  79.     printf("Address stored in numberPtr = %p\n", numberPtr);
  80.    
  81.     // to get the value at the address pointed to by numberPtr, do:
  82.     printf("Value at that address = %d\n", *numberPtr);
  83.     // this is also called "dereferencing" the pointer
  84.     // note that you use the asterisk '*' to get the value at the address
  85.  
  86.     //------------------------------------------------------------------------------------------------
  87.     // >> 3) Manipulating values stored at addresses
  88.     //------------------------------------------------------------------------------------------------
  89.     // you can change the value stored at the address that a pointer points to
  90.     // for example:
  91.      
  92.     *numberPtr = 2;
  93.      
  94.     //      memory table
  95.     /*/----------------------\
  96.      *| Address |  Contents  |
  97.      *|----------------------|
  98.      *|  0x1000 | 0x00000002 |  <--- 8 has been replaced with 2
  99.      *|  0x1004 | 0x00001000 |  <--- pointer (0x1000)
  100.      *\----------------------/
  101.      */
  102.      
  103.     //------------------------------------------------------------------------------------------------
  104.     // >> 4) Pointer arithmetic - adding and subtracting
  105.     //------------------------------------------------------------------------------------------------
  106.     // addresses in C are numeric values. because of this, you can perform arithmetic on pointers.
  107.     // the four arithmetic operations you can use on pointers are: ++, +, --, -
  108.     // below are examples for ++. -- also works the same way.
  109.      
  110.     // for integers (like the above examples), pointer arithmetic works like this:
  111.     numberPtr++;    //pointer increments by 4 because each int is 4 bytes in length
  112.     // note: this is the same as numberPtr = numberPtr + 1;
  113.      
  114.     //      memory table                     memory table
  115.     /*/----------------------\         /----------------------\
  116.      *| Address |  Contents  |         | Address |  Contents  |
  117.      *|----------------------|   +4    |----------------------|
  118.      *|  0x1004 | 0x00001000 |  ---->  |  0x1004 | 0x00001004 |
  119.      *\----------------------/         \----------------------/
  120.      */
  121.      
  122.     // for characters:
  123.     char character = 'C';
  124.     char *characterPtr = NULL;
  125.     characterPtr = &c;
  126.      
  127.     //      memory table
  128.     /*/----------------------\
  129.      *| Address |  Contents  |
  130.      *|----------------------|
  131.      *|  0x1008 |    0x43    |  <--- character (1 byte, 0x43 is the ascii for capital 'C')
  132.      *|  0x1009 | 0x00001008 |  <--- characterPtr
  133.      *\----------------------/
  134.      */
  135.      
  136.     characterPtr++; //pointer increments by 1, because char is 1 byte
  137.     // note: this is the same as characterPtr = characterPtr + 1;
  138.      
  139.     //      memory table                     memory table
  140.     /*/----------------------\         /----------------------\
  141.      *| Address |  Contents  |         | Address |  Contents  |
  142.      *|----------------------|   +1    |----------------------|
  143.      *|  0x1009 | 0x00001008 |  ---->  |  0x1009 | 0x00001009 |
  144.      *\----------------------/         \----------------------/
  145.      */
  146.      
  147.     //------------------------------------------------------------------------------------------------
  148.     // >> 5) Pointer arithmetic - comparisons
  149.     //------------------------------------------------------------------------------------------------
  150.     // pointers can be compared as well, using ==, <, and >
  151.     // you can use this to find out if two address are the same
  152.     // or if they're different, where they are in relation to each other
  153.      
  154.     if ( numberPtr < characterPtr )
  155.         printf("number's address is located BEFORE character's address");
  156.     else if ( numberPtr > characterPtr )
  157.         printf("number's address is located AFTER character's address");
  158.     else if ( numberPtr == characterPtr )
  159.         printf("number and character both share the SAME address!");
  160.      
  161.     // the above code should output the first statement, since
  162.     // numberPtr is pointing to 0x00001004, while
  163.     // characterPtr's is pointing to 0x00001009
  164.     // i.e. 1004 < 1009
  165.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement