Created
June 19, 2016 14:10
-
-
Save unleashtheginger/be91dfcb7968c46ce025a4c1be38eccb to your computer and use it in GitHub Desktop.
Just some (terrible) revision on how structures and pointers work. Next time lists and trees. :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| void some_pointers() | |
| { | |
| int i = 1; | |
| int *ptr_i; | |
| printf("%d is an integer.\n",i); | |
| printf("%p is a pointer to a memory location.\n",ptr_i); | |
| printf("And has the value: %d.\n",*ptr_i); | |
| ptr_i = &i; | |
| printf("%p is a pointer which now points to i.\n",ptr_i); | |
| printf("So it of course has the value of %d.\n",*ptr_i); | |
| } | |
| void structures_are_cool() | |
| { | |
| printf("Structures are cool."); | |
| struct s_testy { | |
| char *name; | |
| int age; | |
| }; | |
| struct s_testy testy; | |
| testy.name = "A Man"; | |
| testy.age = 29; | |
| printf("Testy's name is %s and they are %d.\n",testy.name, testy.age); | |
| struct s_testy * ptr_s; | |
| printf("s_ptr points to %p.\n",ptr_s); | |
| ptr_s = &testy; | |
| printf("s_ptr now points to %p.\n",ptr_s); | |
| printf("ptr_s has the values:\n%s.\n%d.\n",ptr_s->name,ptr_s->age); | |
| ptr_s->name = "Cat"; | |
| ptr_s->age = 1; | |
| printf("testy now has the values:\n%s.\n%d.\n",testy.name,testy.age); | |
| } | |
| int main(int argc, char** argv) | |
| { | |
| printf("Hello World -- Sunday 19th June 2016 13:50\n"); | |
| printf("Just setup tmux and vim to be sensible - 13:56\n"); | |
| printf("Now we call some_pointers()\n"); | |
| //some_pointers(); | |
| //some_pointers(); | |
| printf("And now we have some structures - 14:16\n"); | |
| structures_are_cool(); | |
| printf("And that works. :)\n"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment