Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- // Pointers extra review
- int main()
- {
- // A pointer is meant to "point to" an address.
- // Maybe at the address there is something physical?
- // We don't know, but all we know that a pointer is
- // a finger that points to an address or let's say an area somewhere.
- // Here is a stack of potatoes.
- // We declared it and it exists, but it has no values yet,
- // but we know that this char can hold an array of 10 characters.
- // char potatoes[10];
- // Here we initialize the potatoes as "red".
- // In thus, potatoes are manipaulated and can be changed.
- // Am I right?
- char potatoes[10] = { 'r','e','d','\0' };
- // Now we declare the potato pointer.
- char* potatoesPtr = potatoes;
- // If we were to print the potatoesPtr, it would give us the char array
- // of potatoes[10]. Am I right?
- std::cout << potatoesPtr << std::endl;
- // Now what if we added some numbers and manipulated it.
- // What if we dereference it?
- std::cout << potatoesPtr + 1 << std::endl;
- std::cout << *(potatoesPtr + 1) << std::endl;
- // fix up later
- int i = 5;
- int p[3] = { 1,2,3 };
- int* t = p;
- std::cout
- << t[1] << std::endl
- << p[1] << std::endl
- << t + 1 << std::endl
- << *(t + 1);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement