Skip to main content

Advanced Pointer Topics


Pointers to Pointers

We introduced the concept of a pointer to a pointer previously. You can have a pointer to a pointer of any type.
Consider the following:

char ch;  /* a character */
char *pch; /* a pointer to a character */
char **ppch; /* a pointer to a pointer to a character */
We can visualise this in Figure 11.1. Here we can see that **ppch refers to memory address of *pch which refers to the memory address of the variable ch. But what does this mean in practice?
Memory address

Fig. 11.1 Pointers to pointers Recall that char * refers to a (NULL terminated string. So one common and convenient notion is to declare a pointer to a pointer to a string (Figure 11.2)
Pointer to Pointer

Fig. 11.2 Pointer to String Taking this one stage further we can have several strings being pointed to by the pointer (Figure 11.3)
Pointer to String

Fig. 11.3 Pointer to Several Strings We can refer to individual strings by ppch[0], ppch[1], ..... Thus this is identical to declaring char *ppch[].
One common occurrence of this type is in C command line argument input which we now consider.

Command line input

C lets read arguments from the command line which can then be used in our programs.
We can type arguments after the program name when we run the program.
We have seen this with the compiler for example
   c89 -o prog prog.c
c89 is the program, -o prog prog.c the arguments.
In order to be able to use such arguments in our code we must define them as follows:
   main(int argc, char **argv)
So our main function now has its own arguments. These are the only arguments main accepts.

  • argc is the number of arguments typed -- including the program name.
  • argv is an array of strings holding each command line argument -- including the program name in the first array element.
A simple program example:


#include<stdio.h>
 
main (int argc, char **argv)
    { /* program to print arguments
     from command line */
 
     int i;
 
     printf(``argc = %d$\backslash$n$\backslash$n'',argc);
     for (i=0;i<argc;++i)
       printf(``argv[%d]: %s$\backslash$n'',
         i, argv[i]);
   }

Assume it is compiled to run it as args.
So if we type:
   args f1 ``f2'' f3 4 stop! 
The output would be: 


   argc = 6
 
   argv[0] = args
   argv[1] = f1
   argv[2] = f2
   argv[3] = f3
   argv[4] = 4
   argv[5] = stop!


NOTE: $\bullet$ argv[0] is program name.
   $\bullet$ argc counts program name
   $\bullet$ Embedded `` '' are ignored.
   Blank spaces delimit end of arguments.
   Put blanks in `` '' if needed.

Pointers to a Function

 Pointer to a function are perhaps on of the more confusing uses of pointers in C. Pointers to functions are not as common as other pointer uses. However, one common use is in a passing pointers to a function as a parameter in a function call. (Yes this is getting confusing, hold on to your hats for a moment).
This is especially useful when alternative functions maybe used to perform similar tasks on data. You can pass the data and the function to be used to some control function for instance. As we will see shortly the C standard library provided some basic sorting ( qsort) and searching (bsearch) functions for free. You can easily embed your own functions.
To declare a pointer to a function do:

int (*pf) ();
This simply declares a pointer *pf to function that returns and int. No actual function is pointed to yet.
If we have a function int f() then we may simply (!!) write:

pf = &f;
For compiler prototyping to fully work it is better to have full function prototypes for the function and the pointer to a function:

int f(int);
int (*pf) (int) = &f;
Now f() returns an int and takes one int as a parameter.
You can do things like:

ans = f(5);
ans = pf(5);
which are equivalent.
The qsort standard library function is very useful function that is designed to sort an array by a key value of any type into ascending order, as long as the elements of the array are of fixed type.
qsort is prototyped in (stdlib.h):

void qsort(void *base, size_t num_elements, size_t element_size,
   int (*compare)(void const *, void  const *));
The argument base points to the array to be sorted, num_elements indicates how long the array is, element_size is the size in bytes of each array element and the final argumentcompare is a pointer to a function.
qsort calls the compare function which is user defined to compare the data when sorting. Note that qsort maintains it's data type independence by giving the comparison responsibility to the user. The compare function must return certain (integer) values according to the comparison result:
less than zero
: if first value is less than the second value
zero
: if first value is equal to the second value
greater than zero
: if first value is greater than the second value
Some quite complicated data structures can be sorted in this manner. For example, to sort the following structure by integer key:

typedef struct {
        int   key;
        struct other_data;
} Record;
We can write a compare function, record_compare:
int record\_compare(void const *a, void  const *a)
  {  return ( ((Record *)a)->key - ((Record *)b)->key );
  }
Assuming that we have an array of array_length Records suitably filled with date we can call qsort like this:

qsort( array, arraylength, sizeof(Record), record_compare);

Popular posts from this blog

Putimage function in c

putimage function outputs a bit image onto the screen. Declaration:- void putimage(int left, int top, void *ptr, int op); putimage puts the bit image previously saved with getimage back onto the screen, with the upper left corner of the image placed at (left, top). ptr points to the area in memory where the source image is stored. The op argument specifies a operator that controls how the color for each destination pixel on screen is computed, based on pixel already on screen and the corresponding source pixel in memory. c smiling face animation This animation using c draws a smiling face which appears at random position on screen. See output below the code, it will help you in understanding the code easily. C programming code #include<graphics.h> #include<conio.h> #include<stdlib.h>   main() { int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75; void *p;   initgraph(&gd,&gm,"C:\\TC\\BGI");   setcolor(YELLOW)...

C++ Program to find the sum, difference, product and quotient of two integers

#include <iostream.h> #include <conio.h> void main() {   clrscr();   int x = 10;   int y = 2;   int sum, difference, product, quotient;   sum = x + y;   difference = x - y;   product = x * y;   quotient = x / y;   cout << "The sum of " << x << " & " << y << " is " << sum << "." << endl;   cout << "The difference of " << x << " & " << "y <<  is " << difference << "." << endl;   cout << "The product of " << x << " & " << y << " is " << product << "." << endl;   cout << "The quotient of " << x << " & " << y << " is " << quotient << "." << endl;   getch(); }

What are selection statements in C++ ? If and Switch

There are several statements that we can use in C++ programming that help in choosing a set of instructions for using the code depending upon an expression truth value.  What are Selection Statements in C++? As mentioned above there are several statements that we can use to choose a set of pre-defined instructions within a program in any section of the program as per the need. These are called selection statements in C++  In C++ Program there are two types of Selection statements  1. If  2. Switch  The If Statement of C++ To understand "if statement" let's look at an example.  Example - If you have 10 rupees then you can buy the chocolate.  Here, there is a condition in buying chocolate, if you have 10 rupees then only you can buy it.  This shows that the If is a condition which can be true or false, If it is false it won't be executed, in this example, if you don't have 10 rupees i.e. the condition is false you cant buy the chocolate.  So, I...