This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:data:fall2023:projects:ttb0 [2023/09/14 02:50] – [ACCESSING WITH POINTER ARITHMETIC] cfoster8 | notes:data:fall2023:projects:ttb0 [2023/09/14 02:58] (current) – [MALLOC’ED ARRAY] cfoster8 | ||
---|---|---|---|
Line 76: | Line 76: | ||
| | ||
=====MALLOC’ED ARRAY===== | =====MALLOC’ED ARRAY===== | ||
+ | The array exists as a linear allocation of memory, and any particular value in the array can be accessed by adding the value of that location to the pointer when calling it | ||
- | To access the specific parts of your array you need to use (*(ptr)). | + | Example: |
- | The ptr is the pointer | + | |
+ | would make the first x value of the array 20 and | ||
+ | | ||
+ | would make the second x value of the array 30, and so on | ||
====ACCESSING WITH POINTER ARITHMETIC==== | ====ACCESSING WITH POINTER ARITHMETIC==== | ||
+ | Pointers to structs can be accessed the same way a class can, like so | ||
+ | [POINTER NAME].[VARIABLE NAME] = [VARIABLE VALUE] | ||
+ | a more practical example would be | ||
+ | (*b_arr).x = 64 | ||
+ | or some such, it functions as classes would in C++, we just need to make them ourselves since this is just C | ||
=====MALLOC’ED ARRAY OF STRUCTS===== | =====MALLOC’ED ARRAY OF STRUCTS===== | ||