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 7863889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:37:15+00:00 2026-06-02T23:37:15+00:00

I am using this function: int times_on_table(char *search,struct table index[],int wct){ int ct=0,num=0; while(ct<wct){

  • 0

I am using this function:

int times_on_table(char *search,struct table index[],int wct){
 int ct=0,num=0;
 while(ct<wct){
     if(strcmp(search,(index[ct].label))==0) {
         num++;
     }
     ct++;
 }
 return num;
}

to search through an array of structs and find all the times a certain string is stored in the array and returns the number of times the string occurs. Whenever i use this function inside main:

/*EDIT: i had a main from the wrong program my apologies*/

int main(int argc, char **argv){
    int numwds=get_num_words(argv[1]);
    struct table index[numwds];

    int a;
    struct cmd_ops symbol[22];
    store(argv[1],index,numwds);
    ops_gen(symbol);
    int b=times_on_table("in",index,numwds);
    printf("%d",b);
}

the code works fine. However, when i try to use it inside certain functions like this one

struct table* store(char *filename,struct table index[]) {
    FILE *fp;
    fp=fopen(filename,"r");
    char *a;int d=0,e=0,t=0;
    a=malloc(60);
    int wordcount=get_num_words(filename);
    while(d<wordcount){
        fscanf(fp,"%s",a);
        if ((index[d].label=strdup(a))==NULL)
            break;
        index[d].word_num=d;

        times_on_table("this",index,wordcount);/*when i comment this out
                                                 of my code it runs fine*/

        index[d].address=findline(filename,index[d].label,wordcount,index,t);
        d++;
    }
    free(a);
}

the code does not run and gives me a segmentation fault. Any thoughts?

EDIT: I don’t know if this helps but when i get the segfault, it happens before even the first line of code in main is executed.

EDIT:here is the other function that causes a segfault when times_on_table() is called:

int findline(char *filename,char *check,int wordcount,struct table index[],int t){
char *a;
a=malloc(60);
int b=line_count(filename);
int ch;
fpos_t pos;

int line=0,wd=0,loc,s=0,c=1,times;

times=times_on_table(check,index,wordcount);

FILE *fp;
fp=fopen(filename,"r");

int list[wordcount];

while(c<=b){
    fscanf(fp,"%s",a);
    fgetpos(fp,&pos);

    ch=fgetc(fp);ch=fgetc(fp);

    if(strcmp(a,check)==0){
       if(times==0)
            return line;
       else
            times--;
    }

    if(ch==10){
        line++;c++;
    }
    else
        fsetpos(fp,&pos);
    }
    return line;
 }

it was in this function that i first added times_on_table(), and had the segmentation fault keep my program from running.

  • 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-02T23:37:16+00:00Added an answer on June 2, 2026 at 11:37 pm

    Here

    while(d<wordcount){
        fscanf(fp,"%s",a);
        if ((index[d].label=strdup(a))==NULL)
            break;
        index[d].word_num=d;
    
        times_on_table("this",index,wordcount);
    

    you try to count the occurrences of "this" in a wordcount long array, but you have only filled d+1 slots of the array. The other slots may contain garbage, and then accessing index[ct].label is likely to cause a segmentation fault when ct > d.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this function to close existing form and open a new form.
I'm using this function to extract files from .zip archive and store it on
I am using this function https://www.php.net/sys_getloadavg to get a hint about my server load.
Dear all i am using this function to get the GMT time, i am
I'm using this function: preg_match_all('/((\d{9})\.html)/s', $content, $results); It works fine but i just want
so I'm deploying a shipping calculator using this function , and I'm getting this
i am making an upload with Ajaxupload plugin and i am using this function
Currently, I am using this function: function tokenize( str ) local ret = {}
I'm using this simple function: def print_players(players): tot = 1 for p in players:
I am using this jquery function that works fine in win/mac FF 3.5 and

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.