C Programming Tutorial For Beginners

Our programs we’re gonna want to work with memory addresses we’re gonna want to use them in our programs and do certain things with them and those memory addresses are basically called pointers in our programs and when you dereference a pointer essentially what you’re doing is you’re going to the memory address of that pointer and you’re grabbing the piece of information from there all right so.

All points are as our memory addresses and when we dereference a pointer we go to that physical memory address and we.

Get the information or we get the value that’s stored inside of there so I’m gonna show you guys how we can do that and see it’s actually really easy and can be pretty useful so down here I have this integer and it’s an integer variable and it’s storing the integer value 30 and over here I have a pointer variable which is storing the memory address of 30 so it’s storing the memory address of our age variable as you can see right here and down.

Here I’m just printing out this pointer so I’m printing out pH I’m using this percent P and I’m just printing it out to the screen so why don’t we take a look and see what we get so you’ll see over here we just get this memory address probably exactly what you.

Expected to get so over here I want to show you guys how we can dereference a pointer so this pointer variable P age.

Is storing the memory address of this age variable right so when we printed it out we basically were given the physical memory address where this numbered 30 was stored so P age is storing the memory address of 30 it’s storing the memory address where this value is inside of our computer’s memory if I want to dereference a pointer basically what I’m gonna be doing is I’m going to be going to that actual physical memory address and grabbing that piece.

Of information so the way that I can dereference a.

Pointer is by using the asterisks so when I’m printing this point I can dereference it by using this asterisk but now over here since.

I’m dereferencing this pointer I need to print out a number instead of a pointer because when I dereference the pointer this is no longer a pointer this is no longer a memory address now this is gonna be whatever was stored at the memory address the pointer was pointing.

To so this is going to be an integer because this pointer variable was storing the.

When I dereference it this is actually gonna be an integer so let me show you guys I’m gonna print out or I’m gonna run our program and it should print out 30 so you’ll see that we get a 30 so basically what’s happening here is we’re dereferencing the pH pointer variable we’re dereferencing the pH pointer which basically means we’re going and grabbing the actual you know physical value that’s stored at the memory address.

That the pointer was pointing to and that’s why we get 30 and that’s basically what dereferencing is you know.

You’re you have a pointer you can dereference it and you basically.

That it was pointing to it’s essentially all we’re doing so I want to show you guys how we could do this just normally so for example I have this age variable if I just typed this out if I wanted to get the address of this age variable I could use this ampersand.

Right that’s basically what we did up here but what i could do.

Is i could just dereference this so.

I can say ampersand age and then I can put a asterisks here and this is actually gonna dereference that entire thing so now when I run my program it’s gonna print out 30 so it’s not gonna print out the memory address it’s.

Just gonna print out 30 so you can see over here we get 30 so I could actually do this like as many times as I want and this will maybe just illustrate what’s happening so I could put an ampersand here and now.

I’m gonna get that memory address again so you’ll see we’re getting this memory address and I can do the same thing.

So I could dereference this one more time and you’ll see now we’re getting that 30 value again so I mean basically what I’m doing I’m constantly like like here I’m getting the memory address where the age variables stored then I’m dereferencing and getting the value then I’m getting the memory address again and then I’m dereferencing it again so you know hopefully that makes sense when you dereference a pointer you’re basically just getting the value that that pointer is.

Pointing to or you’re getting the value that’s stored in the memory address of the pointer in this tutorial I want to teach you guys how to write.

Two files in C one of the cool things we can do in C is we can actually modify and change and also create new files so in this tutorial I’ll just basically show you guys how to do that we’ll talk about creating files writing files and appending on to files so let’s go ahead and do that down here in my main function we’re.

Actually gonna create a file so I’m going to create a file and I’m gonna store some information inside of that file and the way that we can do that is we can just type out file all in caps just like that and essentially what we’re doing here is we’re creating a pointer to a physical file on our machine.

So I’m just gonna say file and I’m gonna.

Make an Asterix and I’m just gonna give this a name so I’m just gonna call.

This F pointer and that’ll just stand for file.

Pointer and I want to set this equal to F open an F open is actually a function that’s gonna open a file for us and so inside of these parentheses I’m gonna give this a couple of different parameters the first parameter is gonna be the name of the file that I want to.

Open so I’m just gonna call this employees dot text and the second parameter is gonna be a file mode and a file mode is basically just going to tell C what we want to do with the file that.

We’re gonna open and there’s a bunch of different file modes the three most basic types are R and R stands for read so if I put R in there I mean we want to read the file W stands for right so if I put right in there it means I want to either create a new file or write over an existing file.

There’s also a which stands for append and that means I want to append information on the file in this tutorial we’re gonna look at W and a so what I want to do over here is put in W and right now employees dot text this file doesn’t actually exist so what I’m gonna do is I’m actually going to create that file we’re gonna put some information inside that file and then.

That file gets saved onto my machine and in order to do that we’re gonna need this write mode so it’s just going to be a W anytime that we’re open file in see you always want to make sure that you close the.

File so over here I’m saying F open and down here I want to use another.

Function called F close and basically what this is going to do is it’s going to close off the file so it’s gonna remove the file from the memory on our computer and if we do make any changes it’ll kind of like save the file and everything will just.

Be kind of like locked in so you always want to make sure whenever you’re working with a file that you close.

It and inside of this F close function we just want to pass in this F pointer so I’m just gonna say F pointer.

Right there so let’s talk about what’s happening here file is basically just like essentially like a data type I guess and we’re creating a pointer to a physical file on our computer so if you remember a pointer.

Is basically just a memory address right a pointer is a type of data that we can use in our programs it’s essentially just a memory address and this pointer is storing the memory address of this new employees dot text.

File that we’re gonna create so essentially what’s going to happen is we’re gonna create this employee’s dot txt file and F pointer is going to be pointing to the memory address of that file on our computer so that’s essentially what’s happening here and what I want to do is I.

Actually want to write some information to.

This file so actually let me just show you guys really quick what’s happening here so without doing anything.

Else just using this line right here when I run my program a new file called employees dot.

Text is going to get created so over here I’m in my little directory and if you don’t specify an absolute path or like a relative path for.

This it’s just gonna create it in the directory that your C files in.

So if I wanted to create this on my C Drive I could say like C.

Whatever and then like employees out text and this will get created in my C Drive but if I don’t specify like an absolute path or even if I don’t specify a relative path then this is.

Just going to get created in the same directory as our C file and you’ll see over here my main dot C file is right here it’s in this folder so when I run this program let’s watch over here and we can see this employees doubt text file get created so I’m gonna run my program and you’ll see over here that this employees file got created so if I actually bring this out a little bit you’ll see.

It’s a text document its employees dot tax and there’s zero kilobytes inside of this so we haven’t put any information in there but this.

File got created so now let’s talk about putting some info into that file inside this file I’m basically just.

Gonna list out a bunch of like employees that might work in an office and the easiest way to write information to a file is just to say F print F and this is a special function in C which actually works a lot like the printf function so if you’ve used the printf function and.

With this course we’ve been using it throughout the entire course the printf function basically allows you to like print information out onto that console window F printf is it can allow us to essentially just write information to a file I need to give this a couple parameters the first one is gonna be this file pointer in other words I need to tell this function you know what it should be writing to so I’m just gonna say f pointer and then over here we can just give this some information so I can just use.

Like after we put in this file pointer I can just use this like I normally would the printf function so I could just print out like some employees so I could say like let’s say one of the employees is Jim and we’ll put like what they do so.

Like I can say sales men and then we could say actually let’s make a new line and we’ll.

Create another employee Pam and she is the receptionist and then we can make some other employees so.

So now I have three employees that I’m going to basically write out to my employees file so now when I run this program and we can go over here to this employees file you’ll see when I open this up that we.

Have these three lines in here so we have Jim salesman Pam receptionist and Oscar accounting so basically I was able to create this file and write all this information in the file from down here.

Just from down here in this program so that’s pretty cool and I want to.

Show you guys with right so when I’m using this right this is actually going to overwrite a file so for example if I change this to from all.

This text and I just put like overridden in here now when I run this program and we open this employee’s dot text file so let me reopen this you’ll see it just says overridden so I’ve actually overridden all the text that was in there and that’s basically what.

Happens when we just write to a file so let’s go back to what we had before I’m.

Just gonna run this again so we have this text and you’ll see now we’re back with the employees so let’s say that um inside of this file we wanted to add an employee on to the end of it so we wanted to add like a new employee on to this file right well I can use something called append and what append is.

Gonna do is it’s going to allow us to append a new line on to.

The end of this file or actually just append text on to the end of the file instead of saying w I’m just gonna say a and you’ll notice I’m still keeping.

Employees text here and down here inside of this F printf I’m just gonna print out another employee into the file the first thing I’m gonna do is print out a new line because and actually let me show you guys this in.

This employees filed there actually isn’t a line after this last line so the last like position in this file is over here I can’t actually get on to the line below Oscar so I’m gonna want.

New line and this will move the next employee on to the next line.