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 Microsoft uses C#, hello goodbye
@johnstev111 HahaBye lol
@TheForArkLD HaHaHi?
@johnstev111 HaHaHello
@TheForArkLD HaHaHa
technically, it should be either replers
or replitors
(not really but...)
oh okay
Hey I will help with pointers. I can make the tutorial better.
@Jakman HMmm ok, but can you rate it on a scale of 1-10 of how good it is? (1 sucks 10 good)
@HahaYes 420/10
@SpaceFire lol. You found out.
@HahaYes 8 does what it needs to do and more. It could be better though.
@Jakman hmmm I'll send you the project ok?
@HahaYes cool
@Jakman Hey, I just sent it to you. it is called TUTORIAL SERIES MARKDOWN
@HahaYes Im there. Just found it
@Jakman thanks. I will join you
@Jakman yay now you have 300 cycles
@HahaYes nice. I should do one of those specials. That they're always doing.
@Jakman yeah, 300 cycle special would be nice.
@HahaYes cool. I believe I could make a nice Simulator in Rust to show the power of Superior saftey.
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.....
MY OCD!!!!!
@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.
@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?
Keep in mind when working with arrays, it has a built in pointer.
@ipastrano ok thanks, should I continue this series?
@HahaYes Haha! Yes!
I told you that a tutorial on pointers would go well. Good job on being number one on tutorials man.
@Jakman yeah, pointers = profit, also thanks for helping me.
@HahaYes np. I stayed up all night making custom list types in rust with them. And I remembered about this tutorial after i was done this morning.
@Jakman lol XD
@HahaYes do you want me to give you a private course? It will teach you on how to manage your memory correctly
@Jakman hmmmm I'm a competitive programmer, I'm pretty good a memory management. (Cough Cough not really C++ programmer)
@HahaYes what about you join my competition on the ask channel. Read it. You'd like it.
@Jakman hmmm lemme check
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
@Wuru hehe that market share isn't going so good eh
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
@Wuru please be nice, don't call others buffoons. Replit talk is meant to be a place to learn and grow, and while constructive criticism is ok, name-calling is not.
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
@Wuru Sorry, just wanted to clarify that name calling is against the rules.
Well, maybe read up next time. K? @ipastrano
I did know what happened, but like I said in the previous comment, name calling is against the rules not just on a morality scale, but here it can get you banned. @Wuru
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
Just making sure that everyone knows the rules, since people get banned for not following them. I do know that you and HahaYes have resolved it, but in future altercations, please don't resort to name calling. Basically my whole point. @Wuru
I know, but what I'm saying is do not resort to name calling in future disscussions, as it seems to have happened before. @Wuru
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