Char* vs String comparisons

Quick question

Can char* be compared using <, >, and ==. If not should they be converted to strings. I am pretty sure they can be compared, however, I cannot get this line of code to work out right. In a later display function it attempts to access current-> next and crashes as if NULL.

tokens[0] = current-> lastName for reference
tokens[1] = firstName.... etc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
while((current-> next != NULL) && (current-> next-> lastName < newNode-> lastName)) // Traverse the list
   {
	current = current-> next;

	if(current-> lastName == newNode-> lastName)
	 {
	  if(current-> firstName == newNode-> firstName)
	   { 
	    reportDuplicate(tokens[0]);
	   }
	  else if(current-> firstName < newNode-> firstName)//firstName order
	   {
	    newNode-> next = current-> next;
	    current-> next = newNode;
	   } 
     }

	else if(current-> lastName < newNode-> lastName) //lastName order
	 {
	  newNode-> next = current-> next;
	  current-> next = newNode;
	 }
closed account (j3Rz8vqX)
Every character has an ascii decimal value:
http://www.asciitable.com/

'A' being 65 and 'a' being 97.

My question is: are you referring to a c_string or pointer to a character?

If it is a string, you need to string compare; strcmp.

If it is a pointer to single character, you can de-reference then compare.

How about adding:
1
2
3
//After your current = current-> next;//Line 3 on the source provided.
std::cout<<"Current Last Name: "<<current->lastName<<endl;
std::cout<<"New Node Last Name: "<<newNode->lastName<<endl;
Last edited on
Topic archived. No new replies allowed.