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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:10:18+00:00 2026-05-23T05:10:18+00:00

Hello and thanks in advance, In short, what I do is create a pointer

  • 0

Hello and thanks in advance,

In short, what I do is create a pointer which creates a local variable on the Stack and then I request memory by malloc which allocates space in the heap.

Then I create a local int on the stack.

Then I request users input through scanf.

But upon entering the last digit, the console vanishes and does not print me the result even if I try to hold it through getchar()

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 80

int main()
{
    char *student = malloc(MAX_LINE * sizeof(char));
    int grade;

    printf("Enter students name ");
    scanf("%s", student);

    printf("Enter students grade ");
    scanf("%d", grade);// i would say it has to be grade and not &grade
    // it vanishes right after here. Cant get to see any result
     printf("%s received a %d\n", student, grade);

    free(student);
    getchar();



}
  • 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-23T05:10:19+00:00Added an answer on May 23, 2026 at 5:10 am

    scanf("%d", grade); should be scanf("%d", &grade);

    You should pass the address of the variable/buffer where you want to store the input element. And grade refers to the content, there the & addressof operator operating on grade &grade gives us the address of the variable.

    In case of scanf("%s", student); the student itself refers to the address , ie the contents of the pointer variable student which contains the base address of the memory block you just allocated. Therefore this is correct.

    The arguments should always point to the address of the location of the variable/buffer where you want scanf to put that particular section of that input.

    Say initially you have the garbage 0x5964 stored in grade, and the memory address of grade is 0x1234abcd. In scanf ("%d", grade); , scanf would try to write the read in integer into the memory location 0x5964, which is invalid.

    Whereas in scanf ("%d", &grade); , scanf would store the read in integer into the memory location 0x1234abcd. After this referring to grade as r-value would get you the contents on the memory location 0x1234abcd , this is what you want.

       +----------+                                   
       | 0x5964   |  <-------(uses this as address)---+-------------------+
       +----------+  <------+                         |                   |
       |  grade   |     (writes)       scanf ("%d", grade);               |  
       +----------+      (here)        scanf ("%d", &grade);              |
       |0x1234abcd|         |                         |                   |
       +----------+  <------+(uses this as address)---+                   |
                                                                          |
                                                                          |
       +----------+                                                       |
       |  ??????  | <----------(tries to write here)----------------------+
       +----------+
       |   ???    |
       +----------+
       |  0x5964  | 
       +----------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello (and thanks in advance) I'm in a bit of a quandry, I cant
Hello and thanks in advance for the communal help I always find here. I
Hello and thanks in advance. I am retrieving data from the db. The data
Hello all and thanks in advance I have the tables accounts , votes and
Hello and thanks in advance for taking a look at this. I have discovered
Hello and thanks in advance for any help you can provide. My AIR application
Hello and thanks in advance for your help. I have a treeview that is
Hello and thanks for your help in advance. I am starting on the NHibernate
hello and thanks in advance for any help. I am getting an error: 'Syntax
Hello and thanks in advance for the help! I am trying to generate a

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.