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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:29:23+00:00 2026-06-11T18:29:23+00:00

I’m new to C, and I’ve been trying to figure out pointers. This program

  • 0

I’m new to C, and I’ve been trying to figure out pointers.

This program works with -i but segfaults after a few lines and -f segfaults right away.

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

void search_and_print ( char pattern[], FILE* search_file );

int main ( int argc, char *argv[] ) {
        const char TOO_MANY_VARIABLES[] = "Too many arguments from the command line!";
        const char NOT_ENOUGH_VARIABLES[] = "\nUSAGE: a.out [-i] [-f filename] (Search Pattern)\n";

        if (argc < 2) { printf(NOT_ENOUGH_VARIABLES); return(1);}
        // If input
        if (strcmp(argv[1],"-i") == 0) {
                char *pattern = argv[2];
                search_and_print(pattern, stdin);
        }


        // If file
        if (strcmp(argv[1],"-f") == 0) {
                char *pattern = argv[3];
                // Check if file exists
                // Open file
                FILE *file = fopen( argv[2], "r" );
                search_and_print(pattern, file);
                fclose( file );
        }

}

void search_and_print ( char pattern[], FILE* search_file ) {
        // Read through file
        const int MAX_CHARACTERS_PER_LINE = 1000;
        char* line[MAX_CHARACTERS_PER_LINE];
        while  ( fgets(*line, MAX_CHARACTERS_PER_LINE, search_file) != NULL )
                if  ( strstr(*line, pattern) != NULL )
                    printf(*line);
}
  • 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-11T18:29:24+00:00Added an answer on June 11, 2026 at 6:29 pm

    You have quite a few bugs here.

    char* line[MAX_CHARACTERS_PER_LINE];
    

    defines an array of 1000 pointers, not characters. fgets(*line, ... passes the first of those pointers, which is uninitialized, to fgets, most likely causing a segvio.

    printf(*line);
    

    The first argument to printf is a format. Never ever pass user input as the format, as this opens a huge security hole in your program … see http://en.wikipedia.org/wiki/Uncontrolled_format_string

    You should use fputs(line) or printf("%s", line) (once you fix the declaration of line).

    int main
    

    You don’t return a value (except in the error case) … that results undefined behavior.

    FILE *file = fopen( argv[2], "r" );
    

    You should check whether this succeeds. If the file can’t be opened (e.g., it doesn’t exist), passing it to fgets results in undefined behavior.

    if (argc < 2) { printf(NOT_ENOUGH_VARIABLES); return(1);}
    

    This test isn’t sufficient for your -f case.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

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.