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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:03:35+00:00 2026-05-30T09:03:35+00:00

I don’t really know what is wrong with my program. Any idea? #include <stdio.h>

  • 0

I don’t really know what is wrong with my program. Any idea?

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>

#define k_num_of_keywords 35
#define k_length_of_string 10


int main(int argc, const char * argv[]) {

FILE *fp; //holds file pointer
int c; //holds the character as it reads
char *keywords[k_num_of_keywords]; //array of character pointers
int key_counter = 0;

fp = fopen("./keywords", "r");
if(NULL == fp){
    printf("Failed to create file!!\n");
    exit(0);//if file isn't opened exit program because need the file open
}

while((c = fgetc(fp)) != EOF){
//starts reading a char at a time until 1 string is created to compare
//and then it is added to the character pointer array
char temp[k_length_of_string];
short count = 0;

    if(c == '\n') {
        temp[count] = '\0';
        int len = strlen(temp);
        keywords[key_counter] = (char *)malloc(sizeof(char) * (len +1));
        memcpy(keywords[key_counter], temp, len +1);
        key_counter++;
        //now empty temp to reuse it again incase there are more strings left
        int z;
        for(z = 0; z < count; z++)
            temp[z] = '\0';

    } else  {
        temp[count] = c;
        count++;
    }

}//end of while loop
int s;
for(s = 0; s < k_num_of_keywords; s++)
printf("keyword: %s\n", keywords[s]);


}//end of main function

The error I am getting is
“hw1.c: In function ‘main’:
hw1.c:49: error: expected expression before ‘char’”

EDIT: I apologize for how I named the variables at first before this edit. I did not realize I left that. I put that to fix an error I had thinking it wouldn’t work. But now I have another issue. The output of the program after I fix the typo is:

keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
keyword: 
Segmentation fault: 11
  • 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-05-30T09:03:37+00:00Added an answer on May 30, 2026 at 9:03 am

    One thing immediately stands out:

    size0f(char)
    

    Should be

    sizeof(char)
    

    (note the o instead of 0)


    Now that you’ve fixed that, I’d say you’re having problems because temp and count are declared inside your while loop, but they should really be declared before the loop. Don’t forget to reset count when you finish reading a string. Also,

    for(s = 0; s < k_num_of_keywords; s++)
    

    should probably be:

    for(s = 0; s < key_counter; s++)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't really know how to formulate the title, but it should be pretty obvious
(Don't know if this is strictly on-topic, but I don't see any better Stack
I don't know: if this works. if it's a good idea. what it is
Don't know what's wrong here, when I run the application it says Specified method
Don't know if it's I'm doing it wrong or if there's a bug (I
Don't let below code scare you away . The question is really simple, only
Don't know how to google for such, but is there a way to query
I don't know when to add to a dataset a tableadapter or a query
Don't know if I worded the question right, but basically what I want to
Don't know if this has been asked before, so point me to another question

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.