C++ Day 6!: Pointers!
Day 6! of my C++ Tutorial!
Note: I realise that there are now competitors, but please be more nice.
Featured Shoutout: @CodeLongAndPros check out his awesome C Tutorial!
Pointers!
** What are pointers?**
A pointer in C++(and programming in general) is a object that stores a memory address.
Example of a pointer:
#include <iostream> using namespace std; int main () { int var1; char var2[10]; cout << "Address of var1 variable: "; cout << &var1 << endl; cout << "Address of var2 variable: "; cout << &var2 << endl; return 0; }
(This came from Tutorialspoint)
So again, A pointer is a variable whose value is the address of another variable. Like any variable or constant, you must declare a pointer before you can work with it. The general form of a pointer variable declaration is like this"
type *var-name;
Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.
Some Pointer declarations: (I just spelled that so wrong someone tell me how to spell it)
int *ip; // pointer to an integer double *dp; // pointer to a double float *fp; // pointer to a float char *ch // pointer to character
The data type of all these pointers are still those hexadecimal number that show a memory adres.
How to actually use pointers"
#include <iostream> using namespace std; int main () { int var = 42; // actual variable declaration. int *ip; // pointer variable ip = &var; // store address of var in pointer variable cout << "Value of var variable: "; cout << var << endl; // print the address stored in ip pointer variable cout << "Address stored in ip variable: "; cout << ip << endl; // access the value at the address available in pointer cout << "Value of *ip variable: "; cout << *ip << endl; return 0; }
When you run it, it should show something like this:
The Output:
Value of var variable: 42 Address stored in ip variable: 0x7ffca90a4708 Value of *ip variable: 42
Anyways, it is pretty cool.
I'm sorry that I'm kinda bad at pointers. If anyone wants to help me make it better, please contact me in the comments below.
DON'T COPY THE CODE, WRITE IT YOURSELF
Please Upvote!
Another featured repliter
@DynamicSquid THanks for all the shoutouts! :)
FUUUN!
@SpaceFire Beep boop beepity boop!
@SpaceFire YessSSSS Please Upvote!!!!
yes Cpp is like C...
ded
@TheForArkLD Too correct... C# IS BETTER
@johnstev111 btw, can we make something by stekovaya?
@TheForArkLD C++ is what microsoft uses
@HahaYes :why:
@johnstev111 HahaBye lol
@TheForArkLD HaHaHi?
@johnstev111 HaHaHello
@TheForArkLD HaHaHa
Hey I will help with pointers. I can make the tutorial better.
@SpaceFire lol. You found out.
Oh just a tip, don't use std::endl
as it also runs a flush, and can slow performance. Try \n
instead
@DynamicSquid Oh yeah I know it causes a flush, \n is better. I will say a thanks in the next tutorial.
@DynamicSquid Please upvote?
@HahaYes turn this
#include <iostream> using namespace std;
into that
#include <iostream> using namespace std;
and i'll upvote
@DynamicSquid wow picky about spacing.....
@DynamicSquid alright, wishes granted. THanks for all them shoutouts though. # :)
@DynamicSquid Hmmm you have ocd yet you call ocd ocd. It should be called CDO
@DynamicSquid lol 2 more days of school left? Whatttttt lucky
@HahaYes what about you?
@DynamicSquid I have ONE MORE WeEK
@DynamicSquid oh well, I kicked the teacher out of Zoom. I never felt so smart.
@HahaYes how?
@DynamicSquid is this where I say.... HAHAYeSSSSsssSS
@DynamicSquid what do you mean how?
@HahaYes how did you kick the teacher out?
@DynamicSquid oh easy..... you sign in as the teacher.
@HahaYes wait what? so you need the teacher username and password?
@DynamicSquid What happpened In my enlish class the other day, is the teacher accidentally logged of, then secods later said "Mrs. [my teachaer's name] will always return"
@DynamicSquid Using \n
makes code non-portable to windows. That's premature optimization.
@DynamicSquid Write code on Linux, compile on Windows and Linux, run on both, no errors.
@StudentFires oh, I see
Why would you add:
using namespace std;
if you aren't using the standard namespace for your code. It doesn't seem to make sense.
@RayvelArjoon because he used
using namespace std;
so he doesn't need to do
std::cout<< "stuff" << std::endl
@RayvelArjoon namespaces help remove the std:: thing. Also please upvote!
@RayvelArjoon Please upvote!
WheRe cAn i gET mE SoME daY 7?
I told you that a tutorial on pointers would go well. Good job on being number one on tutorials man.
any idea how to generate random numbers with a min/max boundary set?
You missed a lot, how type modifiers work on pointers, what references are, since you showed those off instead of pointers a lot, function pointers... a whole lot more.
You know what, damn you have a new competitor, and I'ma make this look like light-work.
:)
@StudentFires I know what you are saying, I was just giving a "intro" on the next tutorial. Jakman and I are working on a really detailed one for the next day.
@HahaYes Lol, sure.
Mr. @HahaYes I don’t feel so good.
Why?
Cuz I have like 4 fans -_-
And like 5% market share
Mhm, >:( I hope that shoutout from you does my market share a favor @HahaYes
How dare you not mention my Golang tutorial you buffoon
@Wuru why should I? You've been really rude.
Have I, well all rudeness comes from a root maybe you should check yourself. @HahaYes
@Wuru Feel free to upvote!
@Wuru Don't blame others before blaming yourself.
Feel free to be a bit kinder next time. :D @HahaYes
I don’t even know what’s gotten you so upset. I feel like your just salty. @HahaYes
@Wuru I'm not upset or salty, I just had a misunderstanding.
Within what? @HahaYes
@Wuru How about this? We both say sorry and get it over with.
Sure thing. @HahaYes
@Wuru Sorry man
Sorry dude. @HahaYes
@Wuru Yay we've settled it. How about a cup of tea?
Sure thing. And at the pub drinks are on me :D @HahaYes
@Wuru hmmm paying in bitcoin? XD
@Wuru You called him a buffoon, you buffoon
buffoon-ception @johnstev111
@Wuru correct
Stop commenting on subjects you don’t know about. We resolved it. And let me guess you didn’t know that. @ipastrano
The reason why you don’t know is because you didn’t take the time to read. Try to be smarter next time, okay? @ipastrano
Well, maybe read up next time. K? @ipastrano
But, if you would’ve known the full story ya might’ve known that I have already been told that. Maybe before telling me off you can think about the logical way to go about things. @ipastrano
and youre saying im begging for upvotes... lol @HahaYes
@Bookie0 lool. I'm the definition of a hypocrite
Stop pinging me it’s incredibly annoying take this pointless garbage elsewhere. @HahaYes @Bookie0
lol @HahaYes
dude i was just pingning you once @Wuru
And I don’t want it. @Bookie0
well i was just replying to hahayes's comment...
but i'll stop now :) @Wuru
Thank you :D @Bookie0
@Wuru @HahaYes Are we still doing this? I told you both to stop a while ago. Last warning.
@eekboi ummm eekboi why did you give us a warning? This was like a LONG time ago, we've finally stopped so... false warning?
You would be a great twitch mod... @eekboi
@Wuru yeah that might be true...
@Wuru I've actually been thinking about how hard of a job it would be to do that. I mean for like smaller streams it would be very easy, but for people like shroud or huge streamers, I'd fail very very easily.
Bro, it was a joke. @eekboi
@Wuru Yes, I know.
@eekboi lol eekboi pulling out the sarcasm