don't quite understand this line...

This is a discussion on don't quite understand this line... within the C Programming forums, part of the General Programming Boards category; currently reading about lists. i have this line of code in an insertion function. Code: if (lptr->list[pos]>item) i have an ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    25

    don't quite understand this line...

    currently reading about lists.

    i have this line of code in an insertion function.

    Code:
    if (lptr->list[pos]>item)
    i have an array containing this for example:

    0: Deer 1:Fox 2: Dog 3:Cat

    lets say list[pos] is Fox and item is Cat.

    what happens?

    i thought you could only compare strings with the string library functions

    sorry there is no ']' after item. yes they are in the same array
    Last edited by ahming; 06-05-2005 at 01:35 PM.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    list is an array that is a member of some structure pointed to by the pointer lptr and you are comparing the pos element of that array with item using a greater than operator.

    Sure its c and not c++? in c++ operators can be overloaded to do whatever. post more code.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    You must have copied it wrong, or its bad code. There is an unmatched "]" in there.
    Without that it would compare 2 addresses (usually a meaningless action unless in the same array). Try again.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    25
    so i could have instead of

    lptr->list[pos] < item

    could i have

    pos instead of lptr->list[pos]? seeing as it gives the position.

    well here is the full code:

    Code:
    void Insert(List *lptr, char item){
    	int pos, current;
     
    	if(IsFull(lptr))
    	{
    		fprintf(stderr, "List is full.");
    		exit(1);
    	}
     
    	for(pos = 0; pos < lptr->count; pos++)
    	{
    		if(lptr->list[pos] > item)
    			break;
    	}
     
    	current = lptr->count - 1;
    	while (current >= pos)
    	{
    		/* Copy the element into the next position along */
    		lptr->list[current+1] = lptr->list[current];
    		current--;
    	}
     	lptr->list[pos] = item;
    	lptr->count++;
    }
    i want to insert an 'item' say EMU at position 2.
    how does lptr->[pos] < item work if item is not already in the array, seeing as it compares the positions

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    You haven't shown how List is defined, but apparently its something like:
    Code:
    typedef struct {
          ...
          int count;
          char[99] list; /* or some other arbitrary length */
          ...
    } List;
    because your item is a single char. For your code to work, lptr->list[pos] should also be a char, and the validity of the comparison is obvious, but you'll have a hard time getting EMU passed in as an item in a single char.

    I think you copied code that was intended for a different purpose than you are trying to use it. It is now clear what list[pos] and item are, and you are not comparing 2 pointers or addresses, you are comparing 2 chars. All this does is take in characters one at a time and put them in a string keeping the string in ascii value order. (Actually just a char array, because its not nul terminated except by accident.)
    Last edited by Karthur; 06-14-2005 at 11:13 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This isn't correct:
    Code:
    char[99] list; /* or some other arbitrary length */
    It would be:
    Code:
    char list[99]; /* or some other arbitrary length */
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-19-2009, 12:44 AM
  2. Pointer and Polymorphism help.
    By Skyy in forum C++ Programming
    Replies: 29
    Last Post: 12-18-2008, 09:17 PM
  3. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  4. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21