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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:43:49+00:00 2026-06-17T18:43:49+00:00

Possible Duplicate: segmentation fault using scanf I am trying to run a C program

  • 0

Possible Duplicate:
segmentation fault using scanf

I am trying to run a C program wherein I accept password from the user and then output it. However, when I run the program I get a message called “Segmentation Fault (core dumped)”. I know that this fault occurs where the array seems to exceed the stack size but I am unable to figure out where I am wrong. Any help is appreciated. The code is as follows:

int main(int argc, char *argv[])
{
    int i = 0;
    char *password, *key;

int keylength = 256;

    printf("\nPlease enter a password: ");
    scanf(" %[^\n]", &password);
    printf("Entered password is: %s", password);
    return 0;
}
  • 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-17T18:43:50+00:00Added an answer on June 17, 2026 at 6:43 pm

    You’re not allocating any memory for password. It’s just an uninitialized pointer. In C you always need to make sure that your pointers point to a valid allocated memory buffer before using them. Using an uninitialized pointer causes undefined behavior, which usually results in crashes.

    Allocate memory for password by either (1) declaring password as a stack array:

    char password[1024];
    

    or (2), allocate a memory buffer using malloc:

    char *password = malloc(1024);
    

    If you use malloc, remember that anything you allocate with malloc must be deallocated with a corresponding call to free.

    Also, in the code you posted, when you pass your buffer to scanf, you’re taking the address of the pointer itself when you say &password. What you want to do is simply pass the pointer (which is a memory address referring to your allocated buffer), like:

    scanf(" %[^\n]", password);
    

    Notice there is no & before password. You don’t need it because password is a pointer. Placing the & before password means you’re passing a pointer to a pointer, which is not what you want.

    Finally, be aware that when programming in C, buffer overflows are a constant danger. scanf does not do anything to prevent the user of your program from entering more data than can fit in the buffer. If this happens, a buffer overflow occurs and your program will exhibit undefined behavior (and probably crash). There are safer ways to take string input from a user, such as fgets.

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

Sidebar

Related Questions

Possible Duplicate: Why does this Seg Fault? I receive a segmentation fault when using
Possible Duplicate: Getting segmentation fault SIGSEGV in memcpy after mmap I'm using mmap() in
Possible Duplicate: strtok giving Segmentation Fault Why do i get segfault using this code
Possible Duplicate: getting segmentation fault in a small c program Here's my code: char
Possible Duplicate: Why does simple C code receive segmentation fault? I am working in
Possible Duplicate: Why don’t I get a segmentation fault when I write beyond the
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why is this C code causing a segmentation fault? char* string =
Possible Duplicate: C programming, why does this large array declaration produce a segmentation fault?

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.