Thread: How to tell if an array is empty in a structure?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    6

    How to tell if an array is empty in a structure?

    I've got the following structure:

    Code:
    struct word
    	{
    	char theWord[30];
    	int count;
    	};
    struct word arr[15000];
    However, I can't get this for loop to work properly:
    Code:
    for (i=0;strcmp(arr[i].theWord,NULL)!=0 && dupeFlag!=1;i++)
    		{
    		printf("hi %s is the word, %s is temp\n",arr[i].theWord,temp);
    		if (strcmp(arr[i].theWord,temp)==0)
    			dupeFlag=1;
    		}
    //I've also tried, in place of "strcmp(arr[i].theWord,NULL)!=0": arr[i].theWord[0]!='\0', and so on.
    //I don't want to initialize all 15000 arr[x].theWord's to NULL, so is there a way I can detect if they are garbage values or not? This currently gives me a segmentation fault.
    By the way, I'm not allowed to use C++. Can't post the whole code since we get an F if our codes look similar... makes sense but its a bit annoying.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    ==0 and =='\0' almost certainly mean the same thing. But you should test the pointer for NULL.

    There is no need to set all of them to NULL, just the one after the last one written into. Or else you keep a count of the number of strings written.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You cannot tell whether a value is uninitialized or not, no. If you want to check to see whether the strings are empty (that is, have a length of zero), you have to make them empty. You could also create a flag in the struct that signals whether it contains a valid string... but then you'd have to initialize all those flags. So either way, you're going to have to do some sort of initialization that you don't appear to want to. Thankfully, it's very easy to initialize all the values to zero:
    Code:
    struct word arr[15000] = { 0 };
    Now each string is empty (and each "count" member is set to zero).

    As for using strcmp(), you should never pass it NULL; it expects strings, and NULL is not a string. That'd likely be the cause of your segfault. In your case, for strcmp(), you'd compare against ""; but as you noted, comparing the first character against '\0' means the same thing.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    To test a string for NULL:
    Code:
    if (!(arr[i].theWord))
    likewise, "if (arr[i].theWord)" tests not-NULL (which includes "\0\0\0\0\0", so if you use the method recommended by cas above, test "if (arr[i].theWord[0]=='\0')").
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    Thanks guys. Sorry for the late response, thats what I need to know

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 07:53 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM