Hi ,

/* Inputs a list of strings from the keyboard, sorts them */
  /* in ascending or descending order, and then displays them */
  /* on the screen. */
  #include <stdlib.h>
  #include <stdio.h>
 #include <string.h>

  #define MAXLINES 25
  int get_lines(char *lines[]);
  void sort(char *p[], int n, int sort_type);
  void print_strings(char *p[], int n);
  int alpha(char *p1, char *p2);
  int reverse(char *p1, char *p2);

  char *lines[MAXLINES];

  main()
  {
     int number_of_lines, sort_type;

     /* Read in the lines from the keyboard. */

     number_of_lines = get_lines(lines);

     if ( number_of_lines < 0 )
     {
        puts("Memory allocation error");
        exit(-1);
     }

     puts("Enter 0 for reverse order sort, 1 for alphabetical:" );
     scanf("%d", &sort_type);

     sort(lines, number_of_lines, sort_type);
     print_strings(lines, number_of_lines);
     return(0);
  }

  int get_lines(char *lines[])
  {
      int n = 0;
      char buffer[80];  /* Temporary storage for each line. */

      puts("Enter one line at time; enter a blank when done.");

      while (n < MAXLINES && gets(buffer) != 0 && buffer[0] != '\0')
      {
          if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)
          return -1;
          strcpy( lines[n++], buffer );
      }
      return n;

  } /* End of get_lines() */

 void sort(char *p[], int n, int sort_type)
  {
      int a, b;
      char *x;

      /* The pointer to function.  */

      int (*compare)(char *s1, char *s2);

      /* Initialize the pointer to point to the proper comparison */
      /* function depending on the argument sort_type. */

      compare = (sort_type) ? reverse : alpha;

      for (a = 1; a < n; a++)
      {
          for (b = 0; b < n-1; b++)
         {
              if (compare(p[b], p[b+1]) > 0)
              {
                  x = p[b];
                  p[b] = p[b+1];
                  p[b+1] = x;
              }
          }
      }
  }  /* end of sort() */

  void print_strings(char *p[], int n)
  {
      int count;

      for (count = 0; count < n; count++)
          printf("%s\n ", p[count]);
  }

  int alpha(char *p1, char *p2)
  /* Alphabetical comparison. */
  {
      return(strcmp(p2, p1));
  }

  int reverse(char *p1, char *p2)
 /* Reverse alphabetical comparison. */
 {
      return(strcmp(p1, p2));
 }

The source is working but i do not understand how can work in line 68: the function without giving arguments.
Thanks for reading.

Recommended Answers

All 2 Replies

Hi ,

The source is working but i do not understand how can work in line 68: the function without giving arguments.
Thanks for reading.

int (*compare)(char *s1, char *s2);

This is a pointer to a function that would have two parameters strings.

int reverse(char *p1, char *p2); 
int alpha(char *p1, char *p2);

Either of these two functions could be candidates for the compare pointer, since both of these functions accepts the same parameters and returns the same; an integer, just like the pointer to function declaration.

Now to the initialization of the pointer.
it could be like this:
compare = reverse;
or it could be like:
compare = alpha;
Both of these statements initialize compare to point to any of these functions.
However the statement:

compare = (sort_type) ? reverse : alpha;

initializes to one of them according to sort_type which is an int. If is 0 or false compare will point to alpha; if is more than a 0 or true; compare will point to reverse.

>Either of these two functions could be candidates for the compare pointer, since both of these functions accepts the same parameters and returns the same; an integer, just like the pointer to function declaration.

That is really good.


Thank you for confirmation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.