Thread: segmentation error on a strcmp

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    segmentation error on a strcmp

    I'm writing a program and I keep getting a segmentation fault/error on a line that has a strcmp.

    Code:
    void wordCount(char *words[100], int now)
    {
     int  WC[100];
      int w;
      int x;
      int j;
      int i;
      
      for(i=0; i <= now; i++)
      {
       for(j= i+1; j <= now; j++) 
        {
        strcmp(words[i], words[j]);
          if(WC[i]== 0)
          {
           WC[i] = 1;
           if(words[j] == words[i])
            {
    	 WC[i]++;
             WC[j] = -1;
     	 printf("%d:\t%s\n",WC[i],words[i]);
    	}
          }
         
         else if(WC[i] == -1)
          {
           WC[i]++;
           printf("%d:\t%s\n",WC[i], words[i]);
          }
        }
      }  
    }
    If anyone could help me figure out the problem and how to fix it. And I use the function in another .c file that I compile at the same time.

    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by newbie73 View Post
    Code:
     int  WC[100];
    ...  
          if(WC[i]== 0)
    WC is used uninitialized here. Did you mean to perhaps be assigning the return value of the previous strcmp to that?

    It's possible you are running off the end of your arrays here:
    Code:
      for(i=0; i <= now; i++)
      {
       for(j= i+1; j <= now; j++)
    Depending on what now is. If it's 100, then you are going to run off the end of your array.

    Quzah.
    Last edited by quzah; 05-05-2011 at 07:45 PM. Reason: See? Magenta isn't bad at all now is it Tater?
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    4
    How would I fix going off the end of the array?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just make sure that you are passing 99 as the size and not 100, or make sure that you change <= to < if you are passing 100 as the size.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    4
    That's not working

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'm just wondering if you plan to do anything with the return value of that wasted strcmp() call you've got in there...
    Might also be a good idea to initialize wc[] to something...

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    4
    Both of you have mentioned that. How would I initialize it? What am I missing?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by newbie73 View Post
    Both of you have mentioned that. How would I initialize it? What am I missing?
    Code:
    int wc[100] ={0};
    Will set all array elements to 0 ... otherwise they will contain whatever random garbage was in memory when it was created.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp error
    By p595285902 in forum C Programming
    Replies: 18
    Last Post: 03-23-2011, 03:53 PM
  2. struct, string, strcmp(), and "segmentation fault"
    By Roger in forum C Programming
    Replies: 10
    Last Post: 11-07-2009, 10:45 PM
  3. Error with strcmp
    By nick048 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2006, 02:28 PM
  4. Strcmp error
    By Ron in forum C Programming
    Replies: 14
    Last Post: 06-17-2006, 03:06 PM
  5. strcmp error
    By rodrigorules in forum C++ Programming
    Replies: 6
    Last Post: 11-26-2005, 12:54 PM