Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // FILE I/O EXAMPLE - C
- // (c) gigabytemon
- // 04/07/2020
- // 2862 abc5 66cb a008 dbd9 5bf7 0dda 04f8
- // e192 33e4 e689 4efb 24b9 d34f 6c2d 1ee1
- /* introduction
- * ------------
- * every variable declaration is a location in your computer's memory.
- * that location has an address, which is a 8-character long hexadecimal number.
- * for example:
- * int i; <-- this 'i' is a location in memory where an integer is stored
- * so 'i' is a symbolic representation of that location in memory.
- * meanwhile, its proper address could be, say, 0xbff5a400
- *
- * don't worry about the hexadecimal. just understand that it's an address.
- * 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.
- * the only difference is that pointers give you the address of the variable, rather than the value stored inside the variable.
- */
- /* syntax
- * ------
- * the general syntax of a pointer is:
- *
- * type *pointer_name
- * pointer_name = &variable
- *
- * like any variable, the pointer must be declared with a type and a name.
- * the '*' tells the compiler that this declaration is for a pointer.
- * the pointer's type must be the same as the variable you are associating it with
- */
- /* personal notes
- * --------------
- * pointers are very useful for memory management and manipulation.
- * they can store and manage dynamically allocated blocks of memory.
- *
- * i hate them.
- */
- #include <stdio.h>
- void main()
- {
- //------------------------------------------------------------------------------------------------
- // >> 1) Assigning pointers to variables
- //------------------------------------------------------------------------------------------------
- // variable declaration
- int number = 8; // lets assume this variable is stored at address 0x1000
- // the pointer we'll use to point to 'number'. currently set to NULL
- int *numberPtr = NULL; // assume this pointer is stored right after number, i.e. 0x1004
- // memory table
- /*/----------------------\
- *| Address | Contents |
- *|----------------------|
- *| 0x1000 | 0x00000008 | <--- this is where number = 8 is stored
- *| 0x1004 | 0x00000000 | <--- this is the where the pointer is stored (currently set to NULL)
- *\----------------------/
- */
- // now we'll assign the pointer to the 'number' variable
- numberPtr = &number; // 'number' pointer = address of 'number'
- // memory table
- /*/----------------------\
- *| Address | Contents |
- *|----------------------|
- *| 0x1000 | 0x00000008 | <--- number
- *| 0x1004 | 0x00001000 | <--- now the pointer's value is set to 'number''s address (0x1000)
- *\----------------------/
- */
- //------------------------------------------------------------------------------------------------
- // >> 2) Retrieving information related to the pointer
- //------------------------------------------------------------------------------------------------
- // to get the address stored in the pointer numberPtr, do this:
- printf("Address stored in numberPtr = %p\n", numberPtr);
- // to get the value at the address pointed to by numberPtr, do:
- printf("Value at that address = %d\n", *numberPtr);
- // this is also called "dereferencing" the pointer
- // note that you use the asterisk '*' to get the value at the address
- //------------------------------------------------------------------------------------------------
- // >> 3) Manipulating values stored at addresses
- //------------------------------------------------------------------------------------------------
- // you can change the value stored at the address that a pointer points to
- // for example:
- *numberPtr = 2;
- // memory table
- /*/----------------------\
- *| Address | Contents |
- *|----------------------|
- *| 0x1000 | 0x00000002 | <--- 8 has been replaced with 2
- *| 0x1004 | 0x00001000 | <--- pointer (0x1000)
- *\----------------------/
- */
- //------------------------------------------------------------------------------------------------
- // >> 4) Pointer arithmetic - adding and subtracting
- //------------------------------------------------------------------------------------------------
- // addresses in C are numeric values. because of this, you can perform arithmetic on pointers.
- // the four arithmetic operations you can use on pointers are: ++, +, --, -
- // below are examples for ++. -- also works the same way.
- // for integers (like the above examples), pointer arithmetic works like this:
- numberPtr++; //pointer increments by 4 because each int is 4 bytes in length
- // note: this is the same as numberPtr = numberPtr + 1;
- // memory table memory table
- /*/----------------------\ /----------------------\
- *| Address | Contents | | Address | Contents |
- *|----------------------| +4 |----------------------|
- *| 0x1004 | 0x00001000 | ----> | 0x1004 | 0x00001004 |
- *\----------------------/ \----------------------/
- */
- // for characters:
- char character = 'C';
- char *characterPtr = NULL;
- characterPtr = &c;
- // memory table
- /*/----------------------\
- *| Address | Contents |
- *|----------------------|
- *| 0x1008 | 0x43 | <--- character (1 byte, 0x43 is the ascii for capital 'C')
- *| 0x1009 | 0x00001008 | <--- characterPtr
- *\----------------------/
- */
- characterPtr++; //pointer increments by 1, because char is 1 byte
- // note: this is the same as characterPtr = characterPtr + 1;
- // memory table memory table
- /*/----------------------\ /----------------------\
- *| Address | Contents | | Address | Contents |
- *|----------------------| +1 |----------------------|
- *| 0x1009 | 0x00001008 | ----> | 0x1009 | 0x00001009 |
- *\----------------------/ \----------------------/
- */
- //------------------------------------------------------------------------------------------------
- // >> 5) Pointer arithmetic - comparisons
- //------------------------------------------------------------------------------------------------
- // pointers can be compared as well, using ==, <, and >
- // you can use this to find out if two address are the same
- // or if they're different, where they are in relation to each other
- if ( numberPtr < characterPtr )
- printf("number's address is located BEFORE character's address");
- else if ( numberPtr > characterPtr )
- printf("number's address is located AFTER character's address");
- else if ( numberPtr == characterPtr )
- printf("number and character both share the SAME address!");
- // the above code should output the first statement, since
- // numberPtr is pointing to 0x00001004, while
- // characterPtr's is pointing to 0x00001009
- // i.e. 1004 < 1009
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement