Tuesday, July 14, 2009

Why we need pointers in C?

A pointer is the address of a variable rather than the content of it. For example try to figure out the difference between the "the third drawer" (that is, where it is located) and "the content of the third drawer" (what is inside it).





In C algorithms, there are times when we know where our variable is located, and we want to do some arithmetic or manipulation on it. In these cases, the address of the variable (a pointer to it) is more useful than the value of it. More technically, it is named call by reference. As a simple example imagine the difference between





void inc1 (int x)


{


x++ ;


}





and





void inc2 (int * x)


{


(*x)++ ;


}





If we call inc1 (t), the value of t (and int variable) is copied into x, and then the value of x is incremented. But t has not changed. It’s much like I photocopy a page and I give the copy to you and you write something new on your own copy. It is the normal call-by-value function call.





But if we call inc2(%26amp;t), the address (and not the content) of t is copied into pointer x. then the content of x is incremented. Because x has the address of t, it would mean that the content of t is incremented. It’s much like I tell you where the original page is located, and you go to the page, and write something new on it. It is call-by-reference function call.


--------------------------------------...


I added this comment later. You could mean why we need pointers in C and C++ but we don't need them in languages like C# and VB?





Actually, pointers are used in VB and C#, but this use is not as transparent to the programmer as it is in C and C++. Reference as called in VB and C# is a disguised look of pointer with more limited capabilities as comapared to its capabilities in C and C++. Although pointers provide a more powerful capability, it has some potential danger and problem which could be difficult to find. Reference is quite safe, and is adequate for alomost all the situations.

Why we need pointers in C?
Pointers alows you to do low level programming efficiently from C.
Reply:Because they are alot better then using arrays.





Pointers take up less space in memory and can be resized.





However sometimes it just better to use a array if the list is small enough.
Reply:Because of the way programming works in general.





When you develop an application you need to use memory resources. C being and older high level language gives you alot of low level flexibility and control. When you are using a language like C#, Java, Python and the like, they are actually using pointers for non value types.





But C gives you the ability to allocate RAM for your application and you can set a pointer to point to that area, and use it anyway you want, as a string*, char*, int*.





It is not a matter of needing pointers in C, pointers are needed in any good programming language, but C makes the concept of a pointer alot more apparent and gives you the choice to use value types or pointer types.





I would ask ... why would any language need complex value types like structs lol.


No comments:

Post a Comment