A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Understanding PointersA quick guide to C pointers for people having trouble with the concepts involved. |
On Thursday, May 13th 2004 at 02:42 PM By Nick Fletcher (View Profile) ![]() ![]() ![]() ![]() (Rated 4.2 with 33 votes) |
||
|
Introduction To Pointers ------------------------------------------------------------------------ - - - - - - - Pointers: Pointers can seem scary when you are very new to programming. Sometimes they are not explained very well. I think I can explain them quite well. Here goes. Think of computer memory as a series of letter boxes all lined up. There are millions of them! Each letter box can hold one byte of information. The letter boxes have a little door on the front with a number. This number is different on each letter box. The numbers can get very big because there are so many letterboxes to identify. This means that we will need 'four bytes' to represent the letter box numbers! If this doesn't make sense, think of it this way. If you could only use the numbers 1 and 0 to represent amounts of things how would you do it??? Well, you could represent 0 with 0 and 1 with 1. But that's all. What if you used two columns???
With that, you can see that with a "two bit" number, you can make four combinations. If you increase the number of bits in the number, you can increase the previous amount of combinations by 2. So adding one more bit, to give us a three bit number will allow us to have 8 combinations! Continue that all the way up to one byte (which is eight bits) and you will have 256 possible combinations. So... Getting back to those letterboxes??? I said that they had a four byte number on the front. This means that number will consist of 32 bits, or over 4billion possible combinations! And if your computer is 32bit (most are) it can remember over 4billion letterboxes because it uses a 32bit number to keep track of them(kind of...) Now... Don't get confused between the 'size' of the number used on the front of the letter box and the 'size' of the letter box. The letter box can hold 8 bits or one byte. That means that there can be 4billion letter boxes each contain a value from 0 - 255 (256 combinations right?). (For this discussion, we are going to assume that variables you use in your program are declared as global, or outside of any function ok?) When you write a program and you declare a variable, you are asking the compiler to find a letter box, get it's number and make a table with the letter box number and your variable name next to each other. This way, whenever your varible name appears in your pre, the compiler can check the table and it will know which letterbox has your value in in. Here is where pointers confuse some people. The 32bit number on the front of the letter box is known as it's address. A value can be stored inside that letter box and you can refer to that value by the variable name, or the address on the front of the letter box! Let's go through an example: You write a program and you say
The compiler sees that and opens up a letter box, and places the value 123 inside. It then writes down the address next to the variable name in a table. Later in your program, you wish to use var1 in some kind of operation.
The compiler has to lookup the addresses for var2 and var3. When it has the addresses, it can go to the correct letter box and retrieve the values stored within. It adds the values it has grabbed from the letter boxes (for var2 and var3) and then it looks up in it's table, which address corresponds to the value named var1. It goes to that letterbox and places the result of adding var2 and var3. Whatever was in the letterboxe is replaced. Now some of you will be thinking, "an int variable is 32bits!!", how can you fit 32bits into a letter box that's only big enough for 8bits? Simple. You don't. To store a 32bit number, the complier uses four letterboxes and only remembers the address of the first letterbox. The first letterbox is the one with the lowest memory address. In C++ (and C) you can find the number that's on the front of the letterbox. You use the & (address of) operator. Remember that if your variable is of a type larger than one byte, your varible will be placed in more than one letterbox. You don' have to worry about that, the compiler remembers if it has to look to more than one letterbox or not. There is one import thing to mention in a tutorial about pointers. POINTERS! You declare a pointer like this: int* pointer1; or MyClassIMade* mcim(23,45); or char* a = "This string is being pointed to by a"; The pointer is a variable. It's usually 32bits (four bytes) on most modern PC variants. You know why it's that size don't you? No??? Well, the pointer has to be able to hold any address from 0 to 4billion and we now know that only a 32bit number is capable of that. When you declare the pointer, you tell the compiler what 'type' of pointer. That is how the compiler knows how many letter boxes to use for the value. If that's confusing, think of it this way. If you declare a pointer to and int, you obviously have an int value somewhere in four letterboxes. An int needs four letter boxes because letter boxes can only hold 8bits and your int is 32bits. You have stored your int value and to get a pointer to that value you do this:
What happend there? Well, we ask the compiler to set aside four letter boxes to hold the value 123. The compiler does this and makes a note of the address of the letter boxes and puts the address of the first letter box with the name of your variable in a table. This way, it knows that the two are the same thing. Then, we ask the compiler to make a special variable that can hold a memory address. We tell it that we will need to point to an int that is in memory(letterbox) somewhere. The compiler does what we ask and makes another table entry. This time, it makes a note of how many letter boxes come after the address pointed to by the pointer. In the case of an int, we have the first address and then three more. Only the first address is needed, as long as the compiler knows to get the next three addresses also, everything will work. The next thing we do is the most interesting part of pointers I feel. We tell the compiler to go to it's table and look up myIntValue, and store the letter box number for the first letterbox in the pointer! This means that the pointer now knows where the contents of the variable named myIntValue are actually stored in memory. An very important thing to note here is that there really is no such thing as myIntValue. It's really just a tag that me made up to put on the front of the letterbox. The variable name makes it easier than remembering memory addresses!... The last thing, before I run out of space is, you can use the pointer in two different modes. One mode is for assigning addresses to it, the other is using it to go to the address it holds, open up the letterbox and play with the values in the letterbox! That last mode is known as 'dereferencing' the pointer. You do that by placing the asterix (*) in front of the pointer name. Like this:
That's about all I have time for at this stage. I hope that cleared a few things up! I'll finish with an often used statement used to define what pointers are: A pointer is a variable that holds a memory address Nick. |
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsYeah not bad, hopeing you could show some differnt ways of useing it in a acual situation, but the examples where nice but still i think there is more to cover on pointers
I dont understand !
If had example, I'll say OK !
ok not bad but not clear....
Memory addresses are just numbered places in your computer's memory where you can store information. If you use a variable to store this number, it's called a pointer. If you decide to print the value of this pointer, it will show the memory adress as an integer. You generally don't need to know what address a pointer holds. You do need to know what is at the address it points to though. This is where the '*' dereference operator comes in. It changes 'mode' of the pointer. Once a pointer is 'dereferenced' it will hold the value of whatever is at the memory address is storing.
Hope this helps!
Thanks to give an opertunity to express my opinion.
This doccument is useful for the excellent knowledge in english. But,not use for low level english knowledge. The reason is ur using example is too hard. But, the concept is very nice.............................
I thought it was a very elegant explanation.
It was a good explanation but, it would be nicer to indicate when for example you want to declare a variable and allocate some memory for it. Then fill this memory with the values desired and then point to it when need(called in a function for eg)! That would be an excellent example to clear up everything on pointers because this is a bit more complex.
THE ARTICLE WAS FANTASTICALLY EXPLAINED. THANX NICK! KEEP IT UP! ALTHOUGH GIVING FEW MORE EXAMPLES WOULD HAVE BEEN BETTER,I STILL THINK THIS WAS GOOD ENOUGH COMPARED TO OTHER CONFUSING THINGS SAID ABOUT POINTERS
The concept you chose is very good but the explanations are a bit confusing...
I thought this article was well written for Beginners, enabling them to easily pick up the use of simple pointers.
Hi,
I am just learing C program. I just want to know \"cout >>\" is used for CPP or C program?
the article was good especially to me.i have been experiencing several problems with pointers but now i feel like they are now outdone.thanks alot Nick
Excellent tutorial. Already understand the very basics of C so that helped clarify it alot better. But why couldn\'t all the tutorials be like this! Well explained. It answered the questions i had like the memory address and name, as this initially confused things even more when I first learned about this, but now I completely understand, but not mastered... Yet! =)
Wow, I\'m happy at the response to this article. I\'m planning an updated version that I hope will convey the ideas in an even clearer way.
Thanks for all the comments!
Nick.
hey that was a nice tutorial
can u also give me some idea about making
a file compression project in c/c
basically i want some idea about how to operate with the size of the file
and also give me some easy tutorial for file handling in c/c
hey that was a nice tutorial
can u also give me some idea about making
a file compression project in c/c
basically i want some idea about how to operate with the size of the file
and also give me some easy tutorial for file handling in c/c
it's good.
Thx.
That was really crappy. Too long and too messy to explain something as easy as pointers and references.
thx Nick, I finally got a grasp on pointers. nice comparison.
but why u hav used >> symbol for cout?? it should be << symbol right?
This is the best explanation of pointer i ever know!
Thanks
I am very new to c/c (I've been trying to limit myself to c for the moment until I have a grasp of it and move to c ), this is the first explanation of pointers that I've seen which really gives me an understanding of why they are so useful. I am used to passing values of everything through reference from Python(I learned Python as my first programming language and I now see it as a mistake as the practices and habits I formed are impeding my understanding of c, though I must admit i really enjoy using python). The way you explained pointers shows that c is a lower level language, your are still passing by reference in a way, but it doesn't obscure the steps needed to do that(Python just wraps that functionality into variables, without giving a clear idea of whats happening in the computer). So I just wanted to say thank you, I may reference this when I feel lost... It summarizes everything I have read in a much more cohesive unit, one which I feel I can thoroughly understand, but which opens a new can of worms(how does the computer understand this reference of memory(I guess thats going into architecture, but interesting no less)).
Related Tutorials
Related Source Code
C Job Search