Thread: Help needed in pointer

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    10

    Help needed in pointer

    I have to create a application which reads a textfile and counts the distinct words from it an output to a file with the number of occourences of it. ex:- the 55 , I'm 120 , Went 10 etc..
    I have tried to do it to my knwladge but im now in need of a help of some one. i use a structure to store each word and a pointer to point to the structure. but my compiler (TC) says "Illegal Structure Operation at function updateword" . dev C++ says that i havent declared the "wordlist"(the pointer) please can anyone help me. i have to submit this day after tommorow. im enclosing the codes below.

    Please help me.
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    
    
    #define MAX_WORD  200
    
    struct word_structure{
    	char name[MAX_WORD + 1];
    	int n;
    };
    
    
      int listitems;
      extern int i,add;
      extern char word [MAX_WORD + 1];
      
    
    
    enum _State
    {
      OUTSIDE_WORD,
      INSIDE_WORD
    };
    typedef enum _State State;
    
    
    void UpdateWord(int listitems);
    
    main( int argc, char *argv[] )
    	{
      extern struct word_structure words;
      extern struct word_structure *wordlist;
      FILE       *p_file;              /* The input file. */
      /*char       word [MAX_WORD + 1];  /* The current word. */
      int        l_word;               /* Its length. */
      State      state = OUTSIDE_WORD; /* Are we inside or outside a word. */
      int        c;                    /* The current character read. */
      int        is_delim;             /* Is this character a delimiter. */       
      const char *delims = " \t
    \r`~!@#$&#37;^&*()-=+|\[]{};:",.<>/?"; 
      int        n_total;              /* Total occurrences of all words. */  
      listitems = 0;
    
      wordlist = &words;
     
    /* Open the input file. */
          if (argc < 2)
          {
            printf ("Usage: %s <input file>
    ", argv[0]);
            return (1);
          }
      
      p_file = fopen (argv[1], "r");
       
          if (p_file == NULL)
          {
            printf ("Failed to open '%s'.
    ", argv[1]);
            return (1);
          }
      /* Initialize the tree. */
       /* Read the input file, character by character. */
    
      while ((c = fgetc (p_file)) != EOF)
    
      {
        /* Check whether the current character is a delimiter. */
    
                is_delim = (strchr (delims, c) != NULL);
        /* Act according to the current state. */
                if (state == OUTSIDE_WORD)
                {
                  /* We are outside a word: Ignore any delimiters, but pay attention
                     to the case that a new word has just started. */
                      if (! is_delim)
                      {
                        word[0] = c;
                        l_word = 1;
                        state = INSIDE_WORD;
                      }
                }
                else
                {
                  /* We are inside a word. */
                      if (! is_delim)
                      {
                        /* Current character is not a delimiter - append it to the word. */
                        word[l_word] = c;
                        l_word++;
                      }
                      else
                      {
                        /* We reached the end of the word. */
                        state = OUTSIDE_WORD;
                         /* Create a new WordCount object and update the tree. */
                            if (l_word > 0)
                            {
                              word[l_word] = '\0';                                                 
                              UpdateWord (listitems);   
    
                            }
                      }
                }
      }
       /* Close the input file. */
      fclose (p_file);
      /* Go over all words in the tree and print their number of occurrences. */
      
    
      for(i = 0; i<listitems; i++)
        {
           /*printf("%c  :  %d
    ", *wordlist.name, *wordlist.n);*/
           printf("Im OK");
           wordlist++;
        }
      return (0);
    } 
    
    
    
    void UpdateWord(int listitems)
    {
       
      
            add = 1;
            
                for(i = 0; i<listitems; i++)
                {
                  if( word == wordlist.name)
                  {
                    wordlist.n = *wordlist.n + 1;
                    add = 0;
                    break;
                  }
                  else
                  {
                    ++wordlist;
                  }
                  
                }
                
                if(add)
                {
                  ++wordlist;
                  wordlist.name = word;
                  wordlist.n = 1;
                  listitems++;
                }
    
    }
    Last edited by Salem; 10-15-2007 at 06:52 AM. Reason: snipped email address

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you put "extern ..." inside a function, only that function knows about your external variable, so I guess updateWord wouldn't know about wordlist.

    I suspect
    Code:
    if( word == wordlist.name)
    won't do much good either - you are unlikely to find that your newly read word lives at exactly the same place in memory are the word in your wordlist. Use strcmp().

    Code:
    wordlist.name = word;
    I take it all back - your word will ALWAYS have the same address as the one you've just read. It obviously won't work, but it will have the same address (and thus, always match the most recently read word). This is of course wrong, you should copy the word, not assign it with this way.

    You also seem to have a bunch of newlines within the strings in your printf strings - don't do that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    thanx for replying, if i put the code extern outside the main it triggers another errors
    now it says the" n"," name" of wordlist has not been declared
    wt do u do??
    Please help me

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    Code:
    for(i = 0; i<listitems; i++)
                {
                  if( wordlist.name = word)
                  {
                    wordlist.n = *wordlist.n + 1;
                    add = 0;
                    break;
    Code:
    compiler error :-132 C:\TC\1.C `name' has not been declared 
    132 . request for member of non-aggregate type before '=' token 
    134  `n' has not been declared
    please help me. thanx in advance

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What type is wordlist, and how are you supposed to use that type?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    let me explain.i m not an expert , so correct me.
    "word" is a copy of "word_structure" which has two data types. , the word and the number of occourances
    i want to create a list of these words so that i can save all the distinct words in it and count them.
    to do that , I use a pointer to the "word" structure. "worldlist" is the pointer which is a datatype of same "word_stucture".
    i want to save a word to the "wordlist" and increment it, and again save the next word to it. etcc...
    please help me Im online

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    anyway if im doing this wrong, can u please guide me,
    please be explaining , as i dnt knw abt c in depth.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Your use of extern here is incorrect, it indicates that these variables are actually declared elsewhere (in another source file) but referenced here in the current source file. If you don't actually declared a real instance of these variables somewhere you're going to get a linker error. Your program should not have any externs.

    Code:
      const char *delims = " \t\r`~!@#$&#37;^&*()-=+|\\[]{};:\",.<>/?";
    You need to escape that double quote and of course the backslash itself also needs to be escaped. There is also a "newline in constant" error there that needs to be resolved.


    There is also another problem with how you are passing the listitems variable. You need to pass a reference/pointer to this value so the changes to this variable are reflected back in the calling function. Otherwise, the changes in UpdateWord only affect the local copy that is passed in.

    Code:
    enum _State
    Leading underscores followed by an uppercase letter are reserved for the implementation IIRC.
    Last edited by hk_mp5kpdw; 10-15-2007 at 07:14 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    so how should pass the listitems , thanx again or the reply

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    thanx for it

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Wordlist is a pointer to a data structure. How does that change things?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by lokuhetty View Post
    so how should pass the listitems , thanx again or the reply
    Quote Originally Posted by me
    There is also another problem with how you are passing the listitems variable. You need to pass a reference/pointer to this value so the changes to this variable are reflected back in the calling function. Otherwise, the changes in UpdateWord only affect the local copy that is passed in.
    I thought that was reasonably clear.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    ok, obviously im wrong in my logic, please correct me,
    i want to save the words that i extracted in to a some kind of an list, so that i can finally serch
    please tell me a way. thanx all of u for helping me

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    ok, friends, according to ur advices i changed the code to follows
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    
    
    #define MAX_WORD  200
    
    struct word_structure{
    	char name[MAX_WORD + 1];
    	int n;
    };
    
    
      int listitems;
      int i,add;
      char word [MAX_WORD + 1];
      struct word_structure words;
      struct word_structure *wordlist;
      
    
    
    enum _state
    {
      OUTSIDE_WORD,
      INSIDE_WORD
    };
    typedef enum _state State;
    
    
    void UpdateWord();
    
    main( int argc, char *argv[] )
    	{
    
      FILE       *p_file;              /* The input file. */
      /*char       word [MAX_WORD + 1];  /* The current word. */
      int        l_word;               /* Its length. */
      State      state = OUTSIDE_WORD; /* Are we inside or outside a word. */
      int        c;                    /* The current character read. */
      int        is_delim;             /* Is this character a delimiter. */       
      const char *delims = " \t\n\r`~!@#$&#37;^&*()-=+|\\[]{};:\",.<>/?"; 
      int        n_total;              /* Total occurrences of all words. */  
      listitems = 0;
    
      wordlist = &words;
     
    /* Open the input file. */
          if (argc < 2)
          {
            printf ("Usage: %s <input file>\n", argv[0]);
            return (1);
          }
      
      p_file = fopen (argv[1], "r");
       
          if (p_file == NULL)
          {
            printf ("Failed to open '%s'.\n", argv[1]);
            return (1);
          }
      /* Initialize the tree. */
       /* Read the input file, character by character. */
    
      while ((c = fgetc (p_file)) != EOF)
    
      {
        /* Check whether the current character is a delimiter. */
    
                is_delim = (strchr (delims, c) != NULL);
        /* Act according to the current state. */
                if (state == OUTSIDE_WORD)
                {
                  /* We are outside a word: Ignore any delimiters, but pay attention
                     to the case that a new word has just started. */
                      if (! is_delim)
                      {
                        word[0] = c;
                        l_word = 1;
                        state = INSIDE_WORD;
                      }
                }
                else
                {
                  /* We are inside a word. */
                      if (! is_delim)
                      {
                        /* Current character is not a delimiter - append it to the word. */
                        word[l_word] = c;
                        l_word++;
                      }
                      else
                      {
                        /* We reached the end of the word. */
                        state = OUTSIDE_WORD;
                         /* Create a new WordCount object and update the tree. */
                            if (l_word > 0)
                            {
                              word[l_word] = '\0';                                                 
                              UpdateWord ();   
    
                            }
                      }
                }
      }
       /* Close the input file. */
      fclose (p_file);
      /* Go over all words in the tree and print their number of occurrences. */
      
    
      for(i = 0; i<listitems; i++)
        {
           /*printf("%c  :  %d\n", *wordlist.name, *wordlist.n);*/
           printf("Im OK");
           wordlist++;
        }
      return (0);
    } 
    
    
    
    void UpdateWord()
    {
       
      
            add = 1;
            
                for(i = 0; i<listitems; i++)
                {
                  if( wordlist.name = word)
                  {
                    wordlist.n = *wordlist.n + 1;
                    add = 0;
                    break;
                  }
                  else
                  {
                    ++wordlist;
                  }
                  
                }
                
                if(add)
                {
                  ++wordlist;
                  strcpy(wordlist.name,word);
                  /*wordlist.name = word;*/
                  wordlist.n = 1;
                  listitems++;
                }
    
    }
    i removed the passing of the "listitems" its a global varible. please guide me.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lokuhetty View Post
    ok, obviously im wrong in my logic, please correct me,
    i want to save the words that i extracted in to a some kind of an list, so that i can finally serch
    please tell me a way. thanx all of u for helping me
    I don't see much wrong with the overall logic in your code - there's some details wrong, but what you are attempting to do seems OK to me.

    You are, however, using a pointer as if it was a struct, and that will throw you the type of errors you are seeing.

    Your variable "words" probably needs to be an array, or you will not be able to store many different words...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer lesson needed
    By verbity in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2007, 01:50 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. file pointer
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 03:52 PM