Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8545725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:51:31+00:00 2026-06-11T12:51:31+00:00

The following program stores every word and then prints them with a number of

  • 0

The following program stores every word and then prints them with a number of occurrences.
Global typedef declaration:

typedef struct {
    char * word;
    int occ;
}
words;
words *data=NULL;

I have a problem with the search function. I’ve created a function returning int that looks like this: (max is the constantly updated size of an array of structures, that’s why I call search function after EOF is reached.)

int search(char *word,int max)
{
    int i;
    for(i=0; i<max; i++)
    {
        if(!strcmp(data[i].word,word)) return i;
    }
    return -1;
}

But I noticed I’m supposed to write a search function having that prototype:

struct abc *find(char *word)

So I’ve created the following code:

struct words *findword(char *word)
{
    struct words *ptr;

    for (ptr = data; ptr != NULL; ptr++) {      /* IS THE STOP CONDITION OK? */
        if (strcmp(word, ptr->word) == 0)
            return ptr;
    }
    return NULL;          

}

And I receive many errors during compilation:

reverse.c: In function ‘findword’:

reverse.c:73: warning: assignment from incompatible pointer type

reverse.c:73: error: increment of pointer to unknown structure

reverse.c:73: error: arithmetic on pointer to an incomplete type

reverse.c:74: error: dereferencing pointer to incomplete type

reverse.c: In function ‘main’:

reverse.c:171: error: ‘which’ undeclared (first use in this function)

reverse.c:171: error: (Each undeclared identifier is reported only once

reverse.c:171: error: for each function it appears in.)

make: * [reverse.o] Error 1


which is an int variable assigned to the return of my firstly written search function.
The error with which is easily fixed, but I don’t know how to replace that (solution working with my base search function):

data[which].occ++;

How to fix it so that it’ll work with my new approach to search?


EDIT

main() added:

int main(int argc, char **argv)
{
    char *word;
    words *temp;
    int c,i,num;
    /*int which;*/
    FILE *infile;

    if(argc!=2) {}      
    if((infile=fopen(argv[1],"r"))==NULL) {}
    num=0;
    while(1)
    {
        c=fgetc(infile);
        if(c==EOF) break;
        if(!isalpha(c)) continue;
        else ungetc(c,infile);
        word=getword(infile);
        word=convert(word);
        /*which=search(word,num);*/ 
        if(findword(word))
        {
            if(!(temp=realloc(data,sizeof(words)*(num+1))))
            {}
            else
                data=temp;
            data[num].word=strdup(word);
            data[num].occ=1;
            num++;
        }
        else
            data[which].occ++;

        free(word);
    }
    sort(num-1);
    for(i=0;i<num;i++)
    {}
    free(data);
    if(fclose(infile))
    {}  
    return 0;
}

I’ve left {} for the irrelevant pieces of code eg. error handling.


EDIT2
The things I’m asking for above, are fixed. However, I get a seg fault now.
I’ll give a link to the whole code, I don’t want to put it in an edited post since it’d create a big mess. Seg fault is caused by lines 73 and 152 (strcmp is not working somehow). Hope that full code will be easier to understand.
FULL CODE

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:51:32+00:00Added an answer on June 11, 2026 at 12:51 pm

    The problems are with your findword function, lets go through all the lines

    struct words *ptr; 
    

    This is not what you ment to do. The typedef you used in defining the structure allows you to not need to write struct anymore. This is why you’re getting the error: reverse.c:73: error: increment of pointer to unknown structure. What you want is just:

    words *ptr;    
    

    Next, the loop:

    for(ptr=data; //This is fine, you're assigning your local ptr to the global data. I assume that's something valid
    
    ptr != NULL; //That could OK too... we can loop while ptr is not NULL
    ptr++)       //This line makes no sense... 
    

    You may want to look up how for loops work again, the point is you’re incrementing something until it hits a condition. ptr++ will move where you’re pointing too, so you’ll no longer be pointing to your structure.

    I need to see your main() function to understand what you’re trying to accomplish, but based on the prototype you have to follow, I think the easiest solution would be something like:

    void main()
    {
        // init your vars
        bool more_words_to_add = true;
        words *ptr = NULL;
        int i;
    
        // populate your array of words
        while(more_words_to_add) {
            for(i = 0; i<max; i++) {
              if(ptr = findword("word"))  //if we find the word
                ptr->occ++;  //increment the number of times we found it
              else {
                //I don't know what you want to do here, it's not clear what your goal is.
                //Add the new word to your array of words and set occ to 1,
                //then increment max because there's one more word in your array?
              }
            }
            //get the next word to fine, or else set more_words_to_add = false to break
        }
    }
    

    If this type of solution is what you’re looking to do, then you can adjust your findwords function to be very simple:

    struct words *findword(char *word)
    {
        words *ptr = data;
        if (strcmp(word, ptr->word) == 0)
            return ptr;
        return NULL;
    }  
    

    EDIT: For your new error I suspect the problem is with your memory allocation, see this short example of using your structure:

    words *findword(char *word)
    {
        words *ptr = data;
        if(strcmp(word, ptr->word) == 0)
          return ptr;
        return NULL;
    }
    
    int main(){
        words *ptr;
    
        data = realloc(data, sizeof(words));
        data->word = "hello";                //DO NOT SKIP THESE LINES
        data->occ = 0;                       //DO NOT SKIP THESE LINES
    
        if(ptr = findword("hello")) {
          ptr->occ++;
          printf("I found %d %s's\n",ptr->occ, ptr->word);
        }
    } 
    
    mike@linux-4puc:~> ./a.out 
    I found 1 hello's
    

    You can see here that you need to alloc some memory for the global structure then you can store data in it and pass pointers to it.

    EDIT 2:

    Your main() code does this:

    if((ptr = findword(word)))
    {
         //do stuff
    }
    else
      ptr->occ++;
    

    That’s not going to work because if findword() fails it returns NULL, so in the if check ptr is set to NULL, then in the else you’re trying to deference NULL. If (and keep in mind I’m not really reading your logic so this is up to you) you really want to increment ptr->occ if a word is not found then you want this instead:

    if(findword(word))
    {
         ptr = findword(word);
         //do stuff
    }
    else
      ptr->occ++; //increments the current ptr's occ, no new ptr was assigned.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In following program . I have one doubt. I have declared one global variable
I have written the following program which connects to two LDAP stores, compares the
The following program expects user input in the mixed fraction format 'whole_numbernumerator/denominator' and assigns
The following program I'm creating will allow the user will input values to create
The following program, compiled with g++ 4.6, yields the error request for member ‘y’
The following program: import multiprocessing,operator f = operator.itemgetter(0) # def f(*a): return operator.itemgetter(0)(*a) if
the following program force quit and crashes, I don't understand why, import android.app.Activity; import
Consider following program: static void Main (string[] args) { int i; uint ui; i
The following program is a basic linked list divided in 3 classes. In the
The following program doesn't build in VS11 beta, gcc 4.5, or clang 3.1 #include

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.