Learn to Code via Tutorials on Repl.it!

← Back to all posts
C++ Day 6!: Pointers!
h
HahaYes (1051)

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

Commentshotnewtop
oignons (261)

Keep in mind when working with arrays, it has a built in pointer.

HahaYes (1051)

@ipastrano ok thanks, should I continue this series?

Jakman (377)

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

HahaYes (1051)

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

Jakman (377)

@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 (377)

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

HahaYes (1051)

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

Jakman (377)

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

SwaroopBappanad (17)

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

StudentFires (341)

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.

:)

HahaYes (1051)

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

Wuru (584)

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

And like 5% market share

HahaYes (1051)

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

Wuru (584)

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

RayvelArjoon (81)

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.

awesome10 (212)

@RayvelArjoon because he used

using namespace std;

so he doesn't need to do

std::cout<< "stuff" << std::endl
HahaYes (1051)

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

awesome10 (212)

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

Jakman (377)

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

HahaYes (1051)

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

Jakman (377)

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

HahaYes (1051)

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

HahaYes (1051)

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

Jakman (377)

@HahaYes Im there. Just found it

HahaYes (1051)

@Jakman thanks. I will join you

HahaYes (1051)

@Jakman yay now you have 300 cycles

Jakman (377)

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

HahaYes (1051)

@Jakman yeah, 300 cycle special would be nice.

Jakman (377)

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

DynamicSquid (2673)

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

HahaYes (1051)

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

DynamicSquid (2673)

@HahaYes turn this

#include <iostream>

using namespace std;

into that

#include <iostream>
using namespace std;

and i'll upvote

HahaYes (1051)

@DynamicSquid wow picky about spacing.....

HahaYes (1051)

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

HahaYes (1051)

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

HahaYes (1051)

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

HahaYes (1051)

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

HahaYes (1051)

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

DynamicSquid (2673)

@HahaYes how did you kick the teacher out?

HahaYes (1051)

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

DynamicSquid (2673)

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

firefish (308)

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

StudentFires (341)

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

StudentFires (341)

@DynamicSquid
using namespace std;

:)

using namespace::std;

DynamicSquid (2673)

@StudentFires wait, wdym by non-portable?

also,

:))))

StudentFires (341)

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

Wuru (584)

How dare you not mention my Golang tutorial you buffoon

HahaYes (1051)

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

Wuru (584)

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

HahaYes (1051)

@Wuru Feel free to upvote!

HahaYes (1051)

@Wuru Don't blame others before blaming yourself.

Wuru (584)

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

Wuru (584)

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

HahaYes (1051)

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

HahaYes (1051)

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

HahaYes (1051)

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

Wuru (584)

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

HahaYes (1051)

@Wuru hmmm paying in bitcoin? XD

firefish (308)

@Wuru You called him a buffoon, you buffoon

oignons (261)

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

Wuru (584)

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

Wuru (584)

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

oignons (261)

@Wuru Sorry, just wanted to clarify that name calling is against the rules.

Wuru (584)

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

oignons (261)

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

Wuru (584)

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

oignons (261)

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

oignons (261)

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

Bookie0 (3284)

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

HahaYes (1051)

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

Wuru (584)

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

Bookie0 (3284)

dude i was just pingning you once @Wuru

Wuru (584)

And I don’t want it. @Bookie0

Bookie0 (3284)

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

eekboi (271)

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

HahaYes (1051)

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

Wuru (584)

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

HahaYes (1051)

@Wuru yeah that might be true...

eekboi (271)

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

Wuru (584)

Bro, it was a joke. @eekboi

HahaYes (1051)

@eekboi lol eekboi pulling out the sarcasm