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

If our goal is to make our sort routine data type independent, one way of doing this is to use pointers to type void to point to the data instead of using the integer data type. As a start in that direction let' modify a few things in the above so that pointers can be used.


To begin with, we' stick with pointers to type integer.


/*----------------------- bubble_3.c -------------------------*/
/* Program bubble_3.c from PTRTUT10.HTM                  6/13/97 */
#include <stdio.h>
int arr[10] = { 3,6,1,2,3,8,4,1,7,2};
void bubble(int *p, int N);
int compare(int *m, int *n);
int main(void)
{
     int i;
     putchar('\n');
     for (i = 0; i < 10; i++)
     {
           printf("%d ", arr[i]);
     }
     bubble(arr,10);
     putchar('\n');
     for (i = 0; i < 10; i++)
     {
           printf("%d ", arr[i]);
     }
     return 0;
}
void bubble(int *p, int N)
{
     int i, j, t;
     for (i = N-1; i >= 0; i--)
     {
           for (j = 1; j <= i; j++)
           {
                if (compare(&p[j-1], &p[j]))
                {
                     t = p[j-1];
                     p[j-1] = p[j];
                     p[j] = t;
                }
           }
     }
}
int compare(int *m, int *n)
{
     return (*m > *n);
}
/*------------------ end of bubble3.c -------------------------*/


Note the changes. We are now passing a pointer to an integer (or array of integers) to bubble(). And from within bubble we are passing pointers to the elements of the array that we want to compare to our comparison function. And, of course we are dereferencing these pointer in our compare() function in order to make the actual comparison. Our next step will be to convert the pointers in bubble() to pointers to type void so that that function will become more type insensitive. This is shown in bubble_4.


/*------------------ bubble_4.c ----------------------------*/
/* Program bubble_4.c from PTRTUT10,HTM               6/13/97 */
#include <stdio.h>
int arr[10] = { 3,6,1,2,3,8,4,1,7,2};
void bubble(int *p, int N);
int compare(void *m, void *n);
int main(void)
{
     int i;
     putchar('\n');
     for (i = 0; i < 10; i++)
     {
          printf("%d ", arr[i]);
     }
     bubble(arr,10);
     putchar('\n');
     for (i = 0; i < 10; i++)
     {
          printf("%d ", arr[i]);
     }
     return 0;
}
void bubble(int *p, int N)
{
     int i, j, t;
     for (i = N-1; i >= 0; i--)
     {
          for (j = 1; j <= i; j++)
          {
                if (compare((void *)&p[j-1], (void *)&p[j]))
                {
                     t = p[j-1];
                     p[j-1] = p[j];
                     p[j] = t;
                }
          }
     }
}
int compare(void *m, void *n)
{
     int *m1, *n1;
     m1 = (int *)m;
     n1 = (int *)n;
     return (*m1 > *n1);
}
/*------------------ end of bubble_4.c ---------------------*/



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