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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:07:53+00:00 2026-05-16T10:07:53+00:00

I intend to store strings into an array of pointers to strings and then

  • 0

I intend to store strings into an array of pointers to strings and then display them as follows :


char *directions[3];
for(i=0;i<3;i++)
scanf("%s",directions[i]);
for(i=0;i<3;i++)
printf("%s",directions[i]);

but when i run this code ,it gives me segmetation fault ,could someone please correct me?

  • 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-16T10:07:53+00:00Added an answer on May 16, 2026 at 10:07 am

    You have an array of size 3 of pointers to characters. These pointers do not point to any valid memory where you could possibly store some of those strings that you are reading in. Trying to write to invalid memory invokes UB. Here, UB manifests in the form of a segmentation fault (most probably because you are trying to write to a location you have no control on).

    Try allocating some memory first: Say a big enough buffer to read in an entire line (or the biggest string that you think you are going to encounter). Read in, allocate a direction array member and then copy it out as follows:

    char *directions[ 3 ];
    const MAX_LINE_SIZE = 256;
    char line[ MAX_LINE_SIZE ];
    
    for (size_t nstr = 0; nstr < 3; ++nstr) {
          if (fgets( line, MAX_LINE_SIZE, stdin ) != NULL) {
               directions[ nstr ] = malloc( strlen( line ) );
               strcpy( directions[ nstr ], line );
          }
          printf( "%s\n", directions[ nstr ] );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 499k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Short answer: Yes Option 1: Server side You can connect… May 16, 2026 at 12:30 pm
  • Editorial Team
    Editorial Team added an answer I may not fully understand your question, but I think… May 16, 2026 at 12:30 pm
  • Editorial Team
    Editorial Team added an answer Add a ^ at the beginning of the regex. It… May 16, 2026 at 12:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

In the below program , I intend to read each line in a file
I have been running into some troubles recently and I think I need your
I am new to to Eclipse RCP and SWT/JFace. I intend to purchase the
I am currently writing my first Android application and I keep running into references
The following code can be found in the NHibernate.Id.GuidCombGenerator class. The algorithm creates sequential
I've been working with php for about a year now and am finally starting
I have a site where, via Android, a user needs to: fill in a
I am trying to index a table in a database using Lucene. I use
I need to write a file format that writes out data to a file
I don't understand how using a 'challenge token' would add any sort of prevention:

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.