C Programming Tutorial For Beginners

So I can just say GPA and grade and you’ll notice for each of these.

This ampersand before I type out the name of.

The variable so now when we run our program we should get a nice little list of all.

Of these variables and their corresponding addresses so you’ll see over here we have age and it’s stored up memory address.

0 0 6 0 FF OC GPA is stored at this memory address and grade is stored at this memory address so if you were to like go into our computer or go into my computer and go to memory.

Address is 0 0 6 0 FF 0 C that’s actually going to store the value of 30 it’s gonna store the value that’s inside of that age variable if you were to go to.

This memory address you would see the value.

Inside of GPA if you’re going to this memory address you would see the letter which was a capital A so basically all of these variables are store at different memory addresses when I want to access the information in the variable I can just refer to the variables name so I could say a age or I could say GPA but when C wants to access the information and the variable it has to use these addresses so C would use this address C would use this.

Address and this address I can just use the variable names and that’s why C is awesome because it allows us to do stuff.

Like that but when C and when our computer wants to access these values it has to use these memory addresses and so that’s why these are useful.

We’re programming and C it can actually be pretty useful to be able to know.

The addresses like the actual physical addresses of these certain variables in memory so a lot of times in C when we want to work with these variables we can just refer to them by.

Name and we can do all sorts of stuff with them we can access them we can modify them but in other circumstances we’re not just gonna want to be able to access the value we’re.

Also gonna want to be able to access their physical memory address in other words we’re gonna want to be able to know like what that address is and we can use that for a bunch of stuff and in future tutorials I’m gonna talk about why that.

Can be useful but for now I really.

Just wanted to kind of give you guys an overview of what memory addresses are how we’re using them in our program and how we can print them.

Out so again I just use this ampersand and then I type the name of the variable and I use this percent P to print it out in this tutorial I want to talk to you guys about pointers in C now a pointer is basically just a type of data that we can use inside of our.

Programs so up until this point in the course we’ve been using a bunch of different types of data right we’ve been using things like integers and an integer is just a whole number we’ve been using things like doubles and a double is a decimal number we’ve been using chars and a char is basically just a character and now I want to introduce you guys to another type of data which is.

Called a pointer and a pointer is basically just a memory address and a memory address it basically refers to a you know a physical address inside of the memory of our computer where we’re storing a value right and I think pointers tend to confuse a lot of people who learn.

About them and this is sort of one of those topics and see that everybody dreads because it’s you know traditionally very confusing to understand but actually pointers are extremely simple and the problem is that.

They just get over complicated so what I want you guys to do is I just want you guys to think of a pointer as a type of data that’s all it is it’s just.

A type of data that we can work with in our programs it doesn’t have to be any more complicated than that right I can.

Work with an integer in my program an integer is a whole number right I can work with a double in my program a double is a decimal number I can work with a char in my program a char is just a character I can work with a pointer in my program a.

Pointer is just a memory address that’s it a pointer is just another type of data that I can use and I can work with inside.

Of my programs and it just happens to be a memory address.

Inside of the memory or the RAM in our computers that’s that’s all it is it’s very simple people overcomplicate pointers and they don’t have to be over complicated because it’s just another type of data just like an integer or a double but instead of being like a whole number or a decimal number it’s a memory address so hopefully that makes sense and I hope you think you guys don’t try to over complicate this too much because it doesn’t have to be so.

Down here in my program I have an integer that I’ve created and it’s called age so I just said in age and inside of this integer I’m storing a whole number I’m storing an integer right third and if you’ve been following along with this course.

In the last tutorial we talked about memory addresses and we talked about how all of these variables are actually storing these values at physical addresses on our computer’s memory so down here I have this print statement and I’m just printing out ages memory address and I’m using this percent P and I’m just printing out ampersand age and we talked about how this ampersand when I put this.

In front of the variable basically it’s going to give me the physical address in memory where this variable in other words where this.

Thermia value is stored so if I run this program you guys will see over here it says ages memory address and it’s this.

Hexadecimal number right this is the physical memory address of eight the age variable what did we just talk about before remember what I told you pointers are memory addresses that’s what they are it’s a type of data a pointer is a type of data in our program and it’s a memory address right when I use this ampersand and I.

Of a variable this is giving me the memory address right.

I’m able to print out the memory address so really this is.

A pointer this is a type of data in our program that is a memory address just like an integer is a type of data that’s a whole.

Number and a double is a type of data that’s a decimal.

Number and a char is a type of data that’s a character a pointer is a type of data that’s a memory address and this right here this guy right here is a pointer so.

Memory address of the age variable that’s it and when I want to print out a pointer I can use this percent P and it’ll allow me to print it out onto the screen and everyone’s happy so essentially what I’m doing here is I’m printing out a pointer when I say percent P I’m telling this print F function that I want to put.

On a pointer and then I give it a pointer over here I give it a physical memory address that’s what a pointer is now we understand and hopefully understand that a pointer is just a type of data right it’s just a memory address and we can access the memory addresses of specific variables by using this ampersand and then type the name of the variable and that can be pretty useful.

Now that we understand that I want to show you guys how we can work with these pointers so over here I have an integer variable called age and this integer variable is storing an integer value 30 I’m gonna say that one.

More time I have an integer variable and this integer variable is storing.

An integer value 30 I could also create a double variable so I could create a double called GPA and inside of this double variable I could store a double value like 3.4 I could also create a char variable called grade and inside of this char variable I can store a character a I could.

Also create a pointer variable and inside of that pointer variable I could store a pointer okay so just like I stored a character inside of this character variable and I stored a double inside of this double variable I could store a pointer inside of a pointer variable so we can actually store these pointers inside of a pointer variable now here’s where this is going to get a little bit tricky when we create a pointer variable we.

Actually need a physical memory address right so when I create this integer variable like I can just come up with a number right.

Off the top of my head right I can just say like 30.

When I create a double variable I can just store whatever number I want to store but when we create a pointer variable remember we’re gonna be storing a memory address and I.

Don’t necessarily know any memory addresses like right off the top of my head at least I don’t know any meaningful memory addresses so when I create a pointer variable what I want to do.

Is store the memory address of a variable that’s already in a program so I’m gonna say that one more time when I’m creating a pointer variable inside of that pointer variable I’m gonna store the memory address of a another variable inside of our.

Program so let’s say that I wanted to create a pointer that would store.

The memory address of this variable over here so thats or the memory adjusts of age right I can create a pointer just like this so I could.

Say int and asterisks and now I’m gonna give this a name so I’m gonna give this pointer variable a name and remember this pointer variable is gonna store the memory address of the age variable and.

Generally when you’re naming a pointer you’re gonna start with a lowercase P and then you’re gonna type the name of the variable whose memory address you’re storing in the pointer variable so I’m gonna say P age right because this pointer variable is storing the physical memory address of the.

Age variable now what I want to do is set this equal to the memory address of the age variable which we can access using this.

Ampersand so I could say ampersand age so now this pointer variable.

Is storing the memory address of.

The age variable I’m gonna do the same thing down here for this GPA so if I wanted to create a pointer variable that would store the memory address of the GPA variable I could say double asterisks and I’m just gonna say P GPA just like that and I’m gonna set this equal to the memory address of this.

Double variable which we can access using this ampersand and then the name of the variable.

Like that I’m gonna do the same thing down here for this character variable so I want to create a pointer variable which is going to store the memory address of the character variable so I’m just gonna say char Asterix a grade and I’m gonna set this equal to ampersand grade okay so that’s all I’m doing right so now I have this integer variable and I have this pointer variable which is storing the memory location or the memory address of the age variable I have this double variable which is storing a double and I have this.

Pointer variable which is storing a pointer and the pointer.

Just happens to be the memory address of the GPA variable I have this char variable down here and then I have this pointer variable which is storing a pointer which just happens to be the physical memory address of the great variable in.

Our memory okay and that’s basically all you need to know about pointers to get started a pointer is just a type of data it’s just a type of information that we can work with in our programs it just happens to be a memory addresses write an integer is a type of data that we can work.

With in our programs and it just happens.

To be a whole number a double is a type of data that we can work with in our programs and it’s a decimal number a char is a type of data that we can work with in our programs and it’s a character a pointer is a type of data that we can work with in our.

A memory address hopefully that makes sense and just like we create integer variables and double variables we can also create pointer variables the only difference is whenever we create a pointer variable.

We’re gonna store the memory address.

Of another variable in our program so so you’ll see over here when I create.

I’m using the data type of the variable whose address I’m storing so over here when I’m storing the address of an integer variable I’m saying int here when I’m storing the address of a double variable I’m saying double here when I’m storing the address of a char variable I’m saying char here and that’s the basics of using pointers and also creating pointer variables and storing memory addresses inside of variables in this tutorial I want to talk to you guys about dereferencing pointers.

In C now a pointer is basically just a type of data that we can work with in our programs that is a memory address so sometimes in.