Thread: pointers/structers/if statements

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    pointers/structers/if statements

    I am filling a structure with words from a file. When the last word of the file is entered a null character is entered in the next structure member.

    I am then passing the structure to a function with two pointers to the structures. I have a for loop that I want to be broken when one of the pointers points to the null in the structure member.

    I am using an if statement and it does not seem to be working correctly.

    I will post the code that I think is relevant.


    THIS IS THE STRUCTURE AND THE FUNCTION THAT I AM PASSING IT INTO.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    
    #define MAXWORDS 1000
    
    typedef struct
    {
    	char Words[88];
    
    }UniqueWords;
    
    
    void CompareWords(UniqueWords *pCompareWords, UniqueWords *pCompareWords2);
    int main.....

    THIS IS THE FUNCTION WITH THE POINTERS TO THE STRUCTURE THAT I AM PASSING IT INTO. THE IF STATEMENT IN GREEN IS WHERE I EXPECTED THE IF STATEMENT TO BREAK THE FOR LOOP.

    I THOUGHT IT SHOULD BREAK WHEN THE POINTER HIT THE NULL OF THE STRUCTURE, BUT IT IS NOT.

    Code:
    void CompareWords(UniqueWords *pCompareWords, UniqueWords *pCompareWords2)
    {
    	int i;
    	i=0;
    
    	while(CompareWords != '\0')
    	{
    		for(;;)
    		{
    		if(!strcmp(pCompareWords, pCompareWords2))
    			i++;
    			
    		pCompareWords2++;
    		
    		if(pCompareWords2 == '\0')
    			break;
    
    		}
    	}
    	pCompareWords++;
    
    }
    THANKS IN ADVANCE.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Code:
    while(CompareWords != '\0')
    An unfortunate spelling error.
    Wouldn't this have warranted a warning from the compiler?
    Last edited by bernt; 11-25-2010 at 06:56 PM.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I THOUGHT IT SHOULD BREAK WHEN THE POINTER HIT THE NULL OF THE STRUCTURE, BUT IT IS NOT.
    NULL of the structure. What's that?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You can not compare two structs using strcmp(). You must compare structure member:
    Code:
    if(!strcmp(pCompareWords->Words, pCompareWords2->Words))

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    memcmp() will work on structures if they are the same size.

    Code:
    memcmp(pStructa,pStructb,sizeof(structa));
    Although I don't see the reason for a struct to hold a single string... an array or strings makes more sense.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    That's dangerous, CommonTater. Yes in principle, and syntactically, memcmp() works. But structures may have padding between elements whose contents are not guaranteed. Plus, any characters following trailing 0 in string elements may not match when the author intended to only compare "visible" strings.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    That's dangerous, CommonTater. Yes in principle, and syntactically, memcmp() works. But structures may have padding between elements whose contents are not guaranteed. Plus, any characters following trailing 0 in string elements may not match when the author intended to only compare "visible" strings.
    In this case I think I agree... although with properly initialized structs, I use it quite regularly for "has this changed" testing before prompting for save. Of course properly initiliazed means I'm initializing the struct to all 0s.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple if statements and conditions
    By tomeatworld in forum C Programming
    Replies: 19
    Last Post: 11-07-2010, 10:43 AM
  2. Skipping If Statements?
    By CBecker5000 in forum C Programming
    Replies: 1
    Last Post: 11-08-2009, 04:05 PM
  3. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  4. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  5. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM