All tricks in this blog are only for educational purpose. Learn these tricks only for your knowledge. Please donot try these to harm any one. We will not take any responsibility in any case. All softwares and tools on this site are here for private purposes only and If you want to use a software for business purpose, please purchase it. I do not host many of these tools. I Post links that I have found from numerous Search engines. I will not be responsible for any of harm you will do by using these tools.


Thursday, September 23, 2010

Pointers Explained

Over the last few years I have watched various peo-ple who tried to take their first steps with C orC++. I even took the responsibility of teachingthe subject on various occasions. From these experiences I concluded that one of the concepts that novices find most difficult to understand, is point-ers. For this reason I decided to write this article,explaining in simple terms the use of pointers in Cand C++, to help people grasp this powerful con-cept, which is central to these languages. 1 Introduction The C programming language is designed in sucha way, as to provide full control of the underlying machine to the programmer. This design decision,makes C a very powerful programming language,able to be used for tasks that simply cannot bedone with most other high level languages.One of the most powerful tools that C providesis the ability to directly access and manipulate the memory of the computer, through a construct called a pointer. Note that although I am refer-ring to C, everything applies to C++ as well, since pointers are exactly the same in both languages. 2 Memory and Variables First let’s review the way memory is organized ina computer.The memory can be thought of as a big lineararray of bytes, each one identified by an address,which is essentially the index in that big array. Theonly difference is that address 0, as far as C is con-cerned, is not a valid memory location. It is important to understand that the actual address ofany byte in memory is just an integer, and nothing more. 1The CPU can generally access (read or write) anybyte of the main memory, by sending its address to the memory controller in order to “select it” before writing or reading the actual data. 0123456 Address Memory Content 2 32 Figure 1: memory organization in a system with32bit addressing When we create a variable in a C program, for example: int x, the compiler sets aside a block of contiguous memory locations, big enough to fit this variable, and keeps an internal tag that associates the variable name x with the address of the first byte allocated to it. So when we access that vari-able like this: x = 10, the compiler knows where 1Technically, this is not specified by the C standard, and in fact some old 16bit systems used a complex addressing scheme involving segments and offsets. But this is beyond the scope of this introductory document, and in general of no interest to anyone writing C for modern computers, or even for those old computers under most circuimstances.

Download Pointers Explained.


Post a Comment