Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about char **argv (esp for all you Unix guys)

Status
Not open for further replies.

bloggins02

Programmer
Jul 9, 2000
5
US
This might be really basic, but why does this not work:<br><br>int main (int argc, char **argv) <br>{<br>&nbsp;&nbsp;&nbsp;if (argv[1] != &quot;server&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Not a valid argument!\n&quot;);<br>&nbsp;&nbsp;&nbsp;return 0;<br>}<br><br>It never evaluates that argv[1] is equal to &quot;server&quot; even when I run it with &quot;fooprog server&quot;.&nbsp;&nbsp;<br><br>This however does work:<br><br>int main (int argc, char **argv)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;if ((strcmp(argv[1], &quot;server&quot;) != 0)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Not a valid argument!\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br>}<br><br>What's so special about strcmp()?&nbsp;&nbsp;What am I missing here?<br><br>Thanks guys :)
 
Dear bloggins02,<br><br>I'm not a 'UNIX guy' but I believe the time for tolerance has long since passed so...<br><br>When you compare a char* (that is what argv[1] is) to another char* (that's what &quot;server&quot; is) you are comparing the pointers values not the contents of the character arrays. And as you hopefully know, the value of a pointer is an address in memory, if you don't know that you better start cracking some books. So the value of argv[1] might look something like this:<br><br>0x0A001010 and the value of &quot;server&quot; might look something like this:<br>0x0A20080A the two a of course 'not equal'.<br><br>strcmp on the other hand compares the contents of the character arrays passed as parameters, so that's why it works when the two character arrays contain the same data.<br><br>Hope this helps<br>-pete
 
Thanks for clearing that up, and sorry about the &quot;UNIX guy&quot; thing.&nbsp;&nbsp;I was just thinking that command lines are more prevailent in the unix world these days.&nbsp;&nbsp;And yes I do know about pointers, it just doesn't seem like I have the syntax clear yet.&nbsp;&nbsp;I need to review the fact that if you initialize a pointer to:<br><br>int *p;<br>int a;<br><br>p=&a;<br><br>then &quot;p&quot; is the address of what p points to, &p is the address of the pointer itself, and *p dereferences the pointer to mean &quot;the thing to which p points&quot;.<br><br>Ok, I think I got it :)<br><br>Again, thanks, and sorry for thinking aloud again, I just remember it better that way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top