GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Oct-2004, 06:31
yarons yarons is offline
New Member
 
Join Date: Sep 2004
Posts: 5
yarons is on a distinguished road

comparing strings


This one seems to be very easy but I'm struggling with this for a few hours now.
I need to comapre 2 strings: one is in a pointer and the other is constant.

When I write strcmp(*str,"hello"); I get teh following warning : different types for formal and actual parameter 1
This reulting in a runtime error.

Any ideas?

Ron
  #2  
Old 19-Oct-2004, 07:42
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by yarons
This one seems to be very easy but I'm struggling with this for a few hours now.
I need to comapre 2 strings: one is in a pointer and the other is constant.

When I write strcmp(*str,"hello"); I get teh following warning : different types for formal and actual parameter 1
This reulting in a runtime error.

Any ideas?

Ron
Hi Ron. Welcome to GIDForums.

You are passing a char instead of a char* when you write *str.

Try using just strcmp(str,"hello").
  #3  
Old 19-Oct-2004, 07:53
yarons yarons is offline
New Member
 
Join Date: Sep 2004
Posts: 5
yarons is on a distinguished road

I think I'm going blind :)


How could I not see that???!!!

Thanks
  #4  
Old 19-Oct-2004, 07:56
yarons yarons is offline
New Member
 
Join Date: Sep 2004
Posts: 5
yarons is on a distinguished road

one more thing...


Can I use a switch statement to compare strings e.g.
CPP / C++ / C Code:
switch(str)
{
   case "hello":
     bla bla
     break;


}
??
Last edited by dsmith : 19-Oct-2004 at 08:20. Reason: Please use [c] & [/c] for syntax highlighting
  #5  
Old 19-Oct-2004, 08:24
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Unfortunately no. You either need to string together a bunch of if-else statements or if you are looking for a command parser, you could compare one charecter at a time:

CPP / C++ / C Code:
switch(*command){   //Just look at the first charecter...
  case 'e':
    switch(*(command+1)){  //Look at second charecter...
      case 'n':  //It is possible that this is end as this is the only command that starts with "en"
        call_end_function();
        break;
    }
}
  #6  
Old 19-Oct-2004, 21:50
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dsmith
Unfortunately no. You either need to string together a bunch of if-else statements or if you are looking for a command parser, you could compare one charecter at a time:

CPP / C++ / C Code:
switch(*command){   //Just look at the first charecter...
  case 'e':
    switch(*(command+1)){  //Look at second charecter...
      case 'n':  //It is possible that this is end as this is the only command that starts with "en"
        call_end_function();
        break;
    }
}
Ewwwww! Gross!
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #7  
Old 19-Oct-2004, 22:03
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by WaltP
Ewwwww! Gross!

What? You don't like it? It has its charms...

Your commands could be entered up to only the first unique charecters or the whole command...

Not perfect, but certainly not deserving of your EWWWWW!

 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sprintf Multiple Strings paulicus C++ Forum 4 02-Aug-2004 14:14
Including Maps and strings?? maddie C++ Forum 17 05-Jul-2004 07:25
Comparing two arrays Nelly C++ Forum 2 16-Jun-2004 05:38
help on comparing structs nusstu C Programming Language 4 03-Apr-2004 03:22
How do you compare two strings? soccer022483 C++ Forum 9 22-Mar-2004 15:45

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.