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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:03:59+00:00 2026-06-14T00:03:59+00:00

Possible Duplicate: Dynamic String Input – using scanf(%as) strcmp with pointers not working in

  • 0

Possible Duplicate:
Dynamic String Input – using scanf(“%as”)
strcmp with pointers not working in C

Is the following considered good code? Shouldn’t I have used malloc somewhere? I was able to compile this and it worked, but I feel like it shouldn’t have.

#include <stdio.h>

int main (void) {

    char *name;

    printf("Whats your name? ");
    scanf("%s", &name);
    printf("\nyour name is %s", &name);

    return 0;
}

What happens if I want to modify name? How would I go about doing so?

Edit: I am really just looking for the most efficient and correct way to do this using pointers. I am assuming malloc is necessary.

  • 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-14T00:04:00+00:00Added an answer on June 14, 2026 at 12:04 am

    name is a pointer, and &name returns the address of the variable name, so the scanf is putting the name you enter into the pointer itself.

    For example, if you enter ABC then the pointer will be 0x00434241 (if the CPU is little-endian) or 0x41434200 (if the CPU is big-endian), where 0x41 is the character code for ‘A’, 0x42 is the character code for ‘B’, etc.

    You should allocate memory into which the entered name can be stored and then pass a pointer to it to scanf.

    Here’s an example allocating on the stack:

    #include <stdio.h>
    
    #define MAX_NAME_LENGTH 256
    
    int main (void) {
    
        char name[MAX_NAME_LENGTH];
    
        printf("Whats your name? ");
        scanf("%s", name);
        printf("\nyour name is %s", name);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: filter using Q object with dynamic from user? I am working on
Possible Duplicate: Django dynamic model fields Good Morning guys! Scenario is the following. For
Possible Duplicate: Dynamic Resource Loading Android In Android, I can load a string from
Possible Duplicate: How to access object using dynamic key? I have multiple select with
Possible Duplicate: Javascript Objects: Dynamic variable names? I have a json string passed in
Possible Duplicate: Need help in dynamic query with IN Clause I am using SQL
Possible Duplicate: AngularJS + JQuery : How to get dynamic content working in angularjs
Possible Duplicate: PHP safe $_GET or not So I'm going to build a dynamic
Possible Duplicate: Convert String to code in Java Dynamic code execution on Java I
Possible Duplicate: SQL Server dynamic PIVOT query? I have temporary table with following structure:

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.