Home arrow C programming arrow A TUTORIAL ON POINTERS AND ARRAYS IN C - Part 4

Language Translator

Hacking Zone

Hacking Tools
Attacking

Configure Windows

Windows Configuration

Novels

Mix Novels

Human Personality

Body Language
A TUTORIAL ON POINTERS AND ARRAYS IN C - Part 4 PDF Print E-mail
Written by Hemanshu   
Saturday, 29 December 2007
Article Index
A TUTORIAL ON POINTERS AND ARRAYS IN C - Part 4
Page 2
Page 3
Page 4
Page 5

We move now to bubble6.c where we use the same function bubble() that we used in bubble5.c to sort strings instead of long integers. Of course we have to change the comparison function since the means by which strings are compared is different from that by which long integers are compared. And,in bubble6.c we have deleted the lines within bubble() that were commented out in bubble5.c.


/*--------------------- bubble6.c ---------------------*/
/* Program bubble_6.c from PTRTUT10.HTM                 6/13/97 */
#include <stdio.h>
#include <string.h>
#define MAX_BUF 256
char arr2[5][20] = {    "Mickey Mouse",
                        "Donald Duck",
                        "Minnie Mouse",
                        "Goofy",
                        "Ted Jensen" };
void bubble(void *p, int width, int N);
int compare(void *m, void *n);
int main(void)
{
    int i;
    putchar('\n');
    for (i = 0; i < 5; i++)
    {
        printf("%s\n", arr2[i]);
    }
    bubble(arr2, 20, 5);
    putchar('\n\n');
    for (i = 0; i < 5; i++)
    {
        printf("%s\n", arr2[i]);
    }
    return 0;
}
void bubble(void *p, int width, int N)
{
    int i, j, k;
    unsigned char buf[MAX_BUF];
    unsigned char *bp = p;
    for (i = N-1; i >= 0; i--)
    {
        for (j = 1; j <= i; j++)
        {
           k = compare((void *)(bp + width*(j-1)), (void *)(bp +
j*width));
           if (k > 0)
             {
               memcpy(buf, bp + width*(j-1), width);
               memcpy(bp + width*(j-1), bp + j*width , width);
               memcpy(bp + j*width, buf, width);
             }
        }
    }
}
int compare(void *m, void *n)
{
     char *m1 = m;
     char *n1 = n;
     return (strcmp(m1,n1));
}
/*------------------- end of bubble6.c ---------------------*/


But, the fact that bubble() was unchanged from that used in bubble5.c indicates that that function is capable of sorting a wide variety of data types. What is left to do is to pass to bubble() the name of the comparison function we want to use so that it can be truly universal. Just as the name of an array is the address of the first element of the array in the data segment, the name of a function decays into the address of that function in the code segment. Thus we need to use a pointer to a function. In this case the comparison function.
Pointers to functions must match the functions pointed to in the number and types of the parameters and the type of the return value. In our case, we declare our function pointer as:


    int (*fptr)(const void *p1, const void *p2);
Note that were we to write:
     int *fptr(const void *p1, const void *p2);


we would have a function prototype for a function which returned a pointer to type int. That is because in C the parenthesis () operator have a higher precedence than the pointer * operator. By putting the parenthesis around the string (*fptr) we indicate that we are declaring a function pointer.
We now modify our declaration of bubble() by adding, as its 4th parameter, a function pointer of the proper type. It' function prototype becomes:


     void bubble(void *p, int width, int N,
                     int(*fptr)(const void *, const void *));



Last Updated ( Saturday, 29 December 2007 )
 
< Prev   Next >
Your Ad Here

Donate us!!

Enter Amount:

RSS socialnet

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits