Home arrow C++ arrow Page 4 - Pointers In C - A Boon Or A Bane?

Pointers In C - A Boon Or A Bane?

Pointers have been known to cause hours upon hours of frustration for C/C++ programmers, but are they good or bad? Here's Deepak's Insight...

Author Info:
By: Deepak P
Rating: 3 stars3 stars3 stars3 stars3 stars / 31
August 19, 2002
  1. · Pointers In C - A Boon Or A Bane?
  2. · Pointers In C
  3. · Dangling Pointers
  4. · Matrices Are Pointers Too!
  5. · Conclusion

print this article

Pointers In C - A Boon Or A Bane? - Matrices Are Pointers Too!
(Page 4 of 5 )

Another problem arises from the fact that as matrices are pointers to pointers, we do not get enough information on getting a variable and knowing that it points to a matrix. For example, getting a variable of type int [][] does not enable us to get any useful information. If we have to access the individual members we have to have two more parameters -- the number of rows, and number of columns.

This is true in the case of single dimension arrays also, however on getting a single dimension array, a, we are not prohibited from accessing a[0], a[1] etc. However, on getting a of type int[][], an access to a[1][1] results in an error. If a is of type, int[][10] then we can access a[1][1] without any errors occuring. This is because C stores the arrays in row major format.

In C there are no bounds for arrays, so even if we allocate only 10 integer spaces to int * a and call a[100], no error will occur, however that location might be garbage. We can call a[-10] also without the compiler showing any error. a[b] is actually translated to *(a+b), which dereferences the address of a+b pointer values.

Will a[b] and b[a] be the same? They should be, even though b is not an array. Try it out. The lack of bound leads us to an advantage as well as a problem. The dynamic resizing of an array is very easy to accomplish. Just go to the last allocated location and allocate further. But then we are not warned when we access memory beyond our control, which might lead to almost undetectable bugs.

There are no such problems for JAVA, however what methods do we have in place to combat this from a JAVA perspective? 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) JAVA uses primitive data types like int, char etc, which make it an idiotic mixture of object oriented and imperative functionalities.

Does this ongoing discussion suggest that the huge books written about pointers in C, C++ etc are going to be a waste of time to read? No, not at all. Pointers will be taught even at the university level as they are one amongst the concepts that 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?? Even if pointers are going to move out of our curriculum, we are still going to program at a high language level. The new generation students will not learn enough to be aware of the underlying machine structure of programming languages such as C. This is enough of reason to emphasize that the newer generation should not be led to such superficial programming.

Interesting C Code Examples
I recently met with a C question that could be of use to you. Can you predict the output of:

int a=0;

Try it out. The result is evident of the underlying implementation. The output will be 20.

Another piece of C code 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 at last the error was so simple. What is passed in 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 through to b. What was wrong? It should really have been malloc(sizeof(int)*n). This obviously does not show up an error for small values of n, whereas it almost always shows up an error with larger values n's.

Another common error is that of not presenting the l-value, but instead the r-value to the scanf function. The 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.

Another interesting feature that C implements with pointers is that of passing functions 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 function signatures appear to be fairly identical, but they are actually very much different. The first function takes a function that returns int * as an argument, whereas the second version takes a pointer to a functionthat returns int as its argument.

A 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 are made using the dot operator, whereas in C and C++ we have to differentiate between a pointer and an instance. If it is a pointer then we have to use the -> operator and the . operator in most cases of an instance.
blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials