Thread: How do I or is it possible to compare a structure member to a string?

  1. #1
    Registered User lantzvillian's Avatar
    Join Date
    Sep 2010
    Posts
    44

    How do I or is it possible to compare a structure member to a string?

    Hi - with a specific question, I'd like to know if it is possible (I assume it is) to copy out a structures member to a variable and compare it to something else.

    For example I have the following structure:
    Code:
    struct cmd_s {
    char cmd[5];
    char filename[256];
    }
    Once it is filled in I can see that the values are getting sent over my socket correctly, however I want to use an if statement to control my program flow.

    Code:
    	struct cmd_s *cmdrecieved;
    	char bufferd[BUFFER_SIZE];
    	
    	recvfrom(fd, bufferd, BUFFER_SIZE, 0, (struct sockaddr *)&srcaddr, (socklen_t*)&srcaddrSize );
    	
    	cmdrecieved = (struct cmd_s *)bufferd;
    	printf( " before cmd = %s \n", cmdrecieved->cmd);  // This works
    	
           //This is what I would like it to do
    	if (strcmp(cmdrecieved->cmd,"send"))  {
    	         printf( " awesome = %s \n", cmdrecieved->cmd);
    	} else
    	{
    	  printf("she broken");
    	}
    You can see the above code is wrong, but it should illustrate what I was trying to do.

    Any suggestions?

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Other than the fact that strcmp() returns 0 if the strings are equal, that should work.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User lantzvillian's Avatar
    Join Date
    Sep 2010
    Posts
    44
    it doesn't work.... other ideas?

  4. #4
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    Quote Originally Posted by lantzvillian View Post
    it doesn't work.... other ideas?
    So you changed the if statement for the strncmp that NeonBlack pointed out?
    And your printf prints :
    before cmd = send


    Dylan

  5. #5
    Registered User lantzvillian's Avatar
    Join Date
    Sep 2010
    Posts
    44
    yes the if statement prints out " cmd = send " and it prints out "shes broken"

  6. #6
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    Not sure what to tell you, it should work after you fixed the strncmp if statement.

    Maybe post your new code with the fix.

    Dylan

  7. #7
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Quote Originally Posted by NeonBlack View Post
    Other than the fact that strcmp() returns 0 if the strings are equal, that should work.
    Are you really sure, as in "Sure I'm sure" that you fixed it like NeonBlack suggested? Because it sure does not sounds like you did.
    I'm just asking, you know, to be sure.

    I'm with dylan, paste the code with the fix, so maybe we could see why it's still not working.

  8. #8
    Registered User lantzvillian's Avatar
    Join Date
    Sep 2010
    Posts
    44
    Look even something like this doesn't work:


    If I entered # ./program filename get

    Code:
    if (strncmp(argv[2],"get",3) = 0 ) {
    
     printf("this worked");
    
    }

    I don't get why a comparison of this nature wouldn't work....

  9. #9
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    Quote Originally Posted by lantzvillian View Post
    I don't get why a comparison of this nature wouldn't work....
    Because it's wrong

    Code:
    if (strncmp(argv[2],"get",3) == 0 ) {
    Dylan

  10. #10
    Registered User lantzvillian's Avatar
    Join Date
    Sep 2010
    Posts
    44
    hahah totally been staring at this all day - How can you tell? so what is the difference between strcmp vs strncmp?

    One just checks everything and strncmp checks for a specified number of chars?

    Dylan would you mind lending me a hand with a project I am attempting to achieve?

    * Edited for spelling.
    Last edited by lantzvillian; 09-26-2010 at 08:52 PM.

  11. #11
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    The strncmp only compares n chars, so your sample program would also match on
    ./program filename getting.
    see strncmp

    You can send me a private message if you want.

    Dylan

  12. #12
    Registered User lantzvillian's Avatar
    Join Date
    Sep 2010
    Posts
    44
    Ahh sweet deal. I'd PM you if I could.. I keep getting permission errors when I click anyone's profile.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (strncmp(argv[2],"get",3) = 0 )
    Something like this shouldn't even compile, nevermind run.

    You really need to post actual error messages along with actual code. Simply saying "strcmp doesn't work" is pure garbage, since we all know that it DOES work when used correctly.

    > I'd PM you if I could.. I keep getting permission errors when I click anyone's profile.
    a) you need about 10 posts before you can see profiles (I think you're there now).
    b) getting answers from a single source means you risk mis-information and no-one else can either correct the mistakes, or indeed learn from them.
    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.

  14. #14
    Registered User lantzvillian's Avatar
    Join Date
    Sep 2010
    Posts
    44
    Well it did compile on Fedora 13 x64 with gcc 4.3x something. There were no errors, just that the comparison was not working at all. If I had an error I would have posted it.

    I can PM now, thanks.

    And I do use my information mother named Google. However, I find that alot of Cs documentation and API is less centralized in comparison to say Java's or PHP's or Drupal's.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM