← Back to Community
C++ Day 6!: Pointers!
Profile icon
HahaYes

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! :)

Voters
Profile icon
Chise0
Profile icon
arrowandbow
Profile icon
WILLIAMBAEWER
Profile icon
programmeruser
Profile icon
WilliamWu13
Profile icon
Neelanjan0906
Profile icon
SelenaYang1
Profile icon
theRoux
Profile icon
sangsoonoh
Profile icon
oignons
Comments
hotnewtop
Profile icon
JustAWalrus

How dare you not mention my Golang tutorial you buffoon

Profile icon
HahaYes

@Wuru why should I? You've been really rude.

Profile icon
JustAWalrus

Have I, well all rudeness comes from a root maybe you should check yourself. @HahaYes

Profile icon
HahaYes

@Wuru Feel free to upvote!

Profile icon
HahaYes

@Wuru Don't blame others before blaming yourself.

Profile icon
JustAWalrus

Feel free to be a bit kinder next time. :D @HahaYes

Profile icon
JustAWalrus

I don’t even know what’s gotten you so upset. I feel like your just salty. @HahaYes

Profile icon
HahaYes

@Wuru I'm not upset or salty, I just had a misunderstanding.

Profile icon
JustAWalrus

Within what? @HahaYes

Profile icon
HahaYes

@Wuru How about this? We both say sorry and get it over with.

Profile icon
JustAWalrus

Sure thing. @HahaYes

Profile icon
HahaYes

@Wuru Sorry man

Profile icon
JustAWalrus

Sorry dude. @HahaYes

Profile icon
HahaYes

@Wuru Yay we've settled it. How about a cup of tea?

Profile icon
JustAWalrus

Sure thing. And at the pub drinks are on me :D @HahaYes

Profile icon
HahaYes

@Wuru hmmm paying in bitcoin? XD

Profile icon
firefish

@Wuru You called him a buffoon, you buffoon

Profile icon
JustAWalrus

buffoon-ception @johnstev111

Profile icon
firefish

@Wuru correct

Profile icon
JustAWalrus

Stop commenting on subjects you don’t know about. We resolved it. And let me guess you didn’t know that. @ipastrano

Profile icon
JustAWalrus

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

Profile icon
JustAWalrus

Well, maybe read up next time. K? @ipastrano

Profile icon
JustAWalrus

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

Profile icon
Bookie0

and youre saying im begging for upvotes... lol @HahaYes

Profile icon
HahaYes

@Bookie0 lool. I'm the definition of a hypocrite

Profile icon
JustAWalrus

Stop pinging me it’s incredibly annoying take this pointless garbage elsewhere. @HahaYes @Bookie0

Profile icon
Bookie0
Profile icon
Bookie0

dude i was just pingning you once @Wuru

Profile icon
JustAWalrus

And I don’t want it. @Bookie0

Profile icon
Bookie0

well i was just replying to hahayes's comment...
but i'll stop now :) @Wuru

Profile icon
JustAWalrus

Thank you :D @Bookie0

Profile icon
[deleted]

@Wuru @HahaYes Are we still doing this? I told you both to stop a while ago. Last warning.

Profile icon
HahaYes

@eekboi ummm eekboi why did you give us a warning? This was like a LONG time ago, we've finally stopped so... false warning?

Profile icon
JustAWalrus

You would be a great twitch mod... @eekboi

Profile icon
HahaYes

@Wuru yeah that might be true...

Profile icon
[deleted]

@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.

Profile icon
JustAWalrus

Bro, it was a joke. @eekboi

Profile icon
[deleted]

@Wuru Yes, I know.

Profile icon
HahaYes

@eekboi lol eekboi pulling out the sarcasm

Profile icon
SpaceFire

FUUUN!

Profile icon
DynamicSquid

@SpaceFire Beep boop beepity boop!

Profile icon
HahaYes

@SpaceFire YessSSSS Please Upvote!!!!

Profile icon
TheForArkLD

yes Cpp is like C...
ded

Profile icon
firefish

@TheForArkLD Too correct... C# IS BETTER

Profile icon
TheForArkLD
Profile icon
firefish
Profile icon
TheForArkLD

@johnstev111 btw, can we make something by stekovaya?

Profile icon
HahaYes

@TheForArkLD C++ is what microsoft uses

Profile icon
TheForArkLD

@HahaYes :why:

Profile icon
firefish

@HahaYes Microsoft uses C#, hello goodbye

Profile icon
TheForArkLD

@johnstev111 HahaBye lol

Profile icon
firefish

@TheForArkLD HaHaHi?

Profile icon
TheForArkLD

@johnstev111 HaHaHello

Profile icon
firefish
Profile icon
awesome10

technically, it should be either replers or replitors (not really but...)

Profile icon
HahaYes

oh okay

Profile icon
Jakman

Hey I will help with pointers. I can make the tutorial better.

Profile icon
HahaYes

@Jakman HMmm ok, but can you rate it on a scale of 1-10 of how good it is? (1 sucks 10 good)

Profile icon
SpaceFire

@HahaYes 420/10

Profile icon
HahaYes

@SpaceFire lol. You found out.

Profile icon
Jakman

@HahaYes 8 does what it needs to do and more. It could be better though.

Profile icon
HahaYes

@Jakman hmmm I'll send you the project ok?

Profile icon
Jakman
Profile icon
HahaYes

@Jakman Hey, I just sent it to you. it is called TUTORIAL SERIES MARKDOWN

Profile icon
Jakman

@HahaYes Im there. Just found it

Profile icon
HahaYes

@Jakman thanks. I will join you

Profile icon
HahaYes

@Jakman yay now you have 300 cycles

Profile icon
Jakman

@HahaYes nice. I should do one of those specials. That they're always doing.

Profile icon
HahaYes

@Jakman yeah, 300 cycle special would be nice.

Profile icon
Jakman

@HahaYes cool. I believe I could make a nice Simulator in Rust to show the power of Superior saftey.

Profile icon
DynamicSquid

Oh just a tip, don't use std::endl as it also runs a flush, and can slow performance. Try \n instead

Profile icon
HahaYes

@DynamicSquid Oh yeah I know it causes a flush, \n is better. I will say a thanks in the next tutorial.

Profile icon
HahaYes

@DynamicSquid Please upvote?

Profile icon
DynamicSquid

@HahaYes turn this

#include <iostream> using namespace std;

into that

#include <iostream> using namespace std;

and i'll upvote

Profile icon
HahaYes

@DynamicSquid wow picky about spacing.....

Profile icon
DynamicSquid

MY OCD!!!!!

Profile icon
HahaYes

@DynamicSquid alright, wishes granted. THanks for all them shoutouts though. # :)

Profile icon
HahaYes

@DynamicSquid Hmmm you have ocd yet you call ocd ocd. It should be called CDO

Profile icon
HahaYes

@DynamicSquid lol 2 more days of school left? Whatttttt lucky

Profile icon
DynamicSquid

@HahaYes what about you?

Profile icon
HahaYes

@DynamicSquid I have ONE MORE WeEK

Profile icon
DynamicSquid
Profile icon
HahaYes

@DynamicSquid oh well, I kicked the teacher out of Zoom. I never felt so smart.

Profile icon
DynamicSquid
Profile icon
DynamicSquid
Profile icon
HahaYes

@DynamicSquid is this where I say.... HAHAYeSSSSsssSS

Profile icon
HahaYes

@DynamicSquid what do you mean how?

Profile icon
DynamicSquid

@HahaYes how did you kick the teacher out?

Profile icon
HahaYes

@DynamicSquid oh easy..... you sign in as the teacher.

Profile icon
DynamicSquid

@HahaYes wait what? so you need the teacher username and password?

Profile icon
firefish

@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"

Profile icon
xxpertHacker

@DynamicSquid Using \n makes code non-portable to windows. That's premature optimization.

Profile icon
xxpertHacker

@DynamicSquid
using namespace std;

:)

using namespace::std;

Profile icon
DynamicSquid

@StudentFires wait, wdym by non-portable?

also,

:))))

Profile icon
xxpertHacker

@DynamicSquid Write code on Linux, compile on Windows and Linux, run on both, no errors.

Profile icon
DynamicSquid

@StudentFires oh, I see

Profile icon
RayvelArjoon

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.

Profile icon
awesome10

@RayvelArjoon because he used

using namespace std;

so he doesn't need to do

std::cout<< "stuff" << std::endl
Profile icon
HahaYes

@RayvelArjoon namespaces help remove the std:: thing. Also please upvote!

Profile icon
HahaYes

@RayvelArjoon Please upvote!

Profile icon
WILLIAMBAEWER

WheRe cAn i gET mE SoME daY 7?

Profile icon
HahaYes
Profile icon
WILLIAMBAEWER
Profile icon
Jakman

I told you that a tutorial on pointers would go well. Good job on being number one on tutorials man.

Profile icon
HahaYes

@Jakman yeah, pointers = profit, also thanks for helping me.

Profile icon
Jakman

@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.

Profile icon
HahaYes

@Jakman lol XD

Profile icon
Jakman

@HahaYes do you want me to give you a private course? It will teach you on how to manage your memory correctly

Profile icon
HahaYes

@Jakman hmmmm I'm a competitive programmer, I'm pretty good a memory management. (Cough Cough not really C++ programmer)

Profile icon
Jakman

@HahaYes what about you join my competition on the ask channel. Read it. You'd like it.

Profile icon
HahaYes

@Jakman hmmm lemme check

Profile icon
SwaroopBappanad

any idea how to generate random numbers with a min/max boundary set?

Profile icon
xxpertHacker

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.

:)

Profile icon
HahaYes

@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.

Profile icon
xxpertHacker

@HahaYes Lol, sure.

Profile icon
JustAWalrus

Mr. @HahaYes I don’t feel so good.
Why?
Cuz I have like 4 fans -_-

And like 5% market share

Profile icon
HahaYes

@Wuru hehe that market share isn't going so good eh

Profile icon
JustAWalrus

Mhm, >:( I hope that shoutout from you does my market share a favor @HahaYes