Thread: Help understanding conditional operator

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    30

    Help understanding conditional operator

    Code:
    /* Inputs a list of strings from the keyboard, sorts them */
    /* in ascending or descending order, then displays them */
    
    #include <stdio.h>
    #include <string.h>
    #include <process.h>
    #include <malloc.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];
    
    void main(void)
    {
    	int number_of_lines, sort_type;
    
    	/* Read in the lines from keyboard */
    	number_of_lines = get_lines(lines);
    
    	if (number_of_lines < 0)
    	{
    		puts("Malloc 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);
    }
    
    int get_lines(char *lines[])
    {
    	int n = 0;
    	char buffer[80]; /* temp storage for each line */
    	puts("Enter on line at a time; enter 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); /* copy buffer to lines at [n] and increment n */
    	}
    	return n; /* return number of lines entered */
    }
    
    void sort(char *p[], int n, int sort_type)
    {
    	int a, b;
    	char *x;
    
    	/* Pointer to function */
    	int (*compare)(char *s1, char *s2);
    
    	/* Initialize the pointer to point at 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;
    			}
    		}
    	}
    }
    
    void print_strings(char *p[], int n)
    {
    	int count;
    
    	for (count = 0; count < n; count++)
    		printf("\n%s ", 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));
    }
    I'm having a little difficulty understanding the usage of the conditional operator (in blue). The user is supposed to enter 0 for a reverse sort and 1 for an alphabetical sort. If I understand the conditional op, if sort_type is true (i.e. non-zero) then the statement evaluates to reverse, otherwise it evaluates to alpha. But, the value to be entered for reverse is zero - so wouldn't that result in a false, and then set compare to alpha? I'm confused...

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    According to the code pasted, if the function was passed a 0 for sort_type it would call the alpha() function, otherwise it would call the reverse() function.

    However, the alpha() and reverse() function names (or the actual sort algorithm) are misleading. The way the arguments to strcmp() inside these 2 functions are passed results in alpha() doing a descending sort (instinctively "reverse") and reverse() does an ascending sort.

    EDIT: Not to mention the evil definition for main() and the fact that process.h isn't a standard header file and the fact that the program uses gets().

    EDIT2: Just to clarify, you're correct about the conditional operator. The author of the program is just a moron.
    Last edited by itsme86; 08-09-2004 at 09:18 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    30
    If I understand you correctly, the problem I have is not with the logic, but with the fact that the sort function names are actually backwards? Well, that's what you get from a "teach Yourself C in 10 Minutes" book.

    Also, I have no idea why process.h is in there, nor do I see the need for void main(void). In your opinion, what would be the better definition for main()?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    First rule of book buying:
    If you see
    Code:
    void main(void)
    put it down and move on to another book. If you already own it, then burn the book.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    30
    Ahhh, wise advice. The book was purchased for me, and I'm required to use it.

    So...I'll stick with int main(void) according to the FAQ.
    Last edited by Sereby; 08-09-2004 at 09:52 AM.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Sereby
    If I understand you correctly, the problem I have is not with the logic, but with the fact that the sort function names are actually backwards?
    Correct!

    Quote Originally Posted by Sereby
    So...I'll stick with int main(void) according to the FAQ.
    Precisely
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    30
    Ahhh, relief...

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    More weirdness

    > #include <malloc.h>
    malloc is declared in stdlib.h

    > lines[n] = (char *)malloc(strlen(buffer) + 1))
    Do not cast the return result of malloc (see the FAQ)
    If you find you absolutely need to, then it means one of two things
    - your compiler is impossibly old and needs to be updated to something with an ANSI-C badge on it
    - it's actually a C++ compiler, in which case figure out how to make it compile C

    > strcpy(lines[n++], buffer);
    The indentation of this line suggests that it is part of the preceding if()
    It isn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conditional Operator
    By arjunajay in forum C Programming
    Replies: 8
    Last Post: 07-10-2008, 08:17 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Conditional operator ? :)
    By ER in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2001, 03:34 AM