Topic : Pointers- A boon or a bane
Author : Deepak
Page : << Previous 2  
Go to page :

to almost undetectable bugs.  But there are no such problems for JAVA. Then what do we have in place. Even an array is an object and linking in JAVA is dynamic. Hence it makes the code slower. To prevent such degradations of performance even in case of integers etc. JAVA uses primitive data types like int, char etc, which makes it an idiotic mixture of object oriented and imperative functionalities. But it is all done for the comfort of the programmer. To let him enjoy!!!

Does this ongoing discussion suggest that the huge books written about Pointers in C, C++ etc. going to be a waste. No, not at all. Pointers will be taught even at the university level as they are one among the concepts which encourage thoughtful programming. It is one among the best tools to acquaint the beginner with the concepts of programming. People say that children should learn functional programming languages such as LISP first before moving to imperative ones. But is that going to happen?? If even pointers are going to move out of our curriculum, we are going to program at a level very high. The new generation will not learn to be aware of the underlying machine structure. This is enough reason to emphasize that the newer generation should not be led to such superficial programming.

I recently met with a C question which can be of use to you. Can you predict the output of
int a=0;

Try it out. The result is a very great evidence of the underlying implementation. The output will be 20. Think it out!!.

  Another program which caused errors that I came across recently was
int function(int *a,int n)
  int *b=(int *)malloc(sizeof(a));
  for(int i=0;i<n;i++)
  /*Other code*/
  return 0;

I saw that it caused errors and looked at it for perhaps half and hour. Then atlast the error was so simple. What is passed is an array a which currently stores n elements a[0] through a[n-1]. The author wanted to create a copy of a in b before altering a. He allocated as many bytes as sizeof(a) and began backing up the data in a onto b. What was wrong? It should really have been malloc(sizeof(int)*n). This obviously does not show up an error for small n's whereas it almost always shows up an error with larger n's.

  Another common error is that of not presenting the l-value but the r-value to the scanf function. scanf function actually takes integers and hence if presented with a value such as 10 it will try to store the input at that location which will almost always result in an error. I really like to use this phrase "almost always". It is the best tool to emphasize that there might be exceptions.

  Another interesting feature that C implements with pointers is that of passing functiions as parameters. We can pass pointers to functions as parameters to other functions which can then call the function(s) passed to them. An interesting kind of error possible here is during prototype declarations of functions receiving pointers to functions as arguments.
void function(int *passed_function(int));
void function(int (*passed_function)(int));

Both appear to be not too much different, but actually they are very much different. The first function takes as argument a function that returns int * whereas the second version takes as argument a pointer to a function that returns int. How different they are and how easily can they be mistaken.

   A very nice feature of pointers in teaching is that the student has to differentiate between a pointer and an instance (of a structure or a class) while using it. In pointerless languages such as JAVA the references to the variables in a class is made using the dot operator with it's object always whereas in C, C++ etc., we have to differentiate between a pointer and an instance. If it is a pointer we have to use the -> operator and the . operator in case of instance. The student has to distinguish between a pointer and an object (or instance) for better understanding of the concepts of programming.

Deepak  <>

Build: 13-August-2002
Please inform me for any modifications or inclusions that you want to see in this article.

Page : << Previous 2