Off Topic > Off Topic
Programming Megathread
Foxscotch:
--- Quote from: Drydess on April 18, 2017, 10:05:33 PM ---can someone explain to me what a pointer is and how you're supposed to use it? i've really been stuck because i gave up early on c in favor of web design languages, but recently tried to get back into it, and still can't grasp much of the application without knowing pointers
--- End quote ---
the only "web design language" is arguably PHP, which sucks butt
I'd try to explain pointers but I think I'll leave that to someone who would probably be able to provide a better explanation lol
Drydess:
--- Quote from: Foxscotch on April 18, 2017, 10:08:43 PM ---the only "web design language" is arguably PHP, which sucks butt
--- End quote ---
ofc technicalities
Metario:
--- Quote from: Drydess on April 18, 2017, 10:05:33 PM ---can someone explain to me what a pointer is and how you're supposed to use it? i've really been stuck because i gave up early on c in favor of web design languages, but recently tried to get back into it, and still can't grasp much of the application without knowing pointers
--- End quote ---
a pointer is a thing that just points to a value in the programs memory referenced usually via addresses
Meldaril:
--- Quote from: Drydess on April 18, 2017, 10:05:33 PM ---can someone explain to me what a pointer is and how you're supposed to use it? i've really been stuck because i gave up early on c in favor of web design languages, but recently tried to get back into it, and still can't grasp much of the application without knowing pointers
--- End quote ---
I'll try to use a terrible brown townogy.
Imagine there is a very long book shelf (with only one shelf) and many millions of books (each book representing a portion of computer memory being used). Some of these books are much bigger than others and take up more space. A pointer is a very small book that's stored in our bookshelf and it contains only one page, that page simply describes where to find another book. A "pointer-book" doesn't need to tell us where another book is, the page might contain a single "0" which means it points to nothing. This is what's called a null pointer.
Example:
--- Code: ---BOOK *pointer_book = nullptr; // "pointing to nothing"
--- End code ---
It's very convenient if the cover of our "pointer-book" says what it's pointing to. For example, the book it's pointing to could be a book about a particular model of car so we say "this is a pointer-book about where to find a car". This is called static typing and it's used by many languages. If you are lazy you can always say 'Our "pointer-book" points to any book of any type!', in which case, the cover of your book will be called "void" which means your "pointer-book" can be used to point to anything.
--- Code: ---CAR *pointer_book1 = &a_book_on_our_car; // A particular model of car
void *pointer_book2 = &a_book_on_our_car; // A new pointer but telling us where our car is again
pointer_book2 = &a_book_on_bicycles; // Reusing our previous pointer, this is valid because it's a "void pointer"
--- End code ---
What happens if the our "pointer-book" points us to a book that's no longer there? What happens if our "pointer-book" points us to a book that's not about that car anymore but instead about bicycles? These are very bad conditions that can crash and burn our whole book-keeping system (our program).
Metario:
Segmentation fault (core dumped).