开发者

C, pointers, console vanishes upon entering data

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 whi开发者_Python百科ch 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();



}


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  | 
   +----------+


You should use scanf("%d", &grade);. scanf() needs to know where to write data, so it needs address of the variable.


Your comment is incorrect, it needs to be &grade because you need to pass the address of grade so that scanf can modify it through the memory address. If you pass just grade, you're passing scanf the value of grade, which is some random garbage (or possible 0 depending on your compiler) because you didn't initialize it. scanf then thinks that the random number (or 0...) is an address and tries to modify the memory at that address, and that causes a crash.

To make getchar pause the program, call fflush(stdin) after the second call to scanf.


Does it crash or just not print the result?
How does it crash?

Anyway (after adding the address-of operator to grade), you should always check the return value of scanf. In this case it should return 1 if all is well, 0 (or EOF) otherwise.

int chk = scanf("%d", &grade);
if (chk == 1) {
    /* rest of your program */
} else {
    fprintf(stderr, "something went wrong in the scanf.\n");
    fprintf(stderr, "Return value was %d\n", chk);
}

I suspect you input a name with embedded spaces (like "full name"). Doing that will assign "full" to student and leave " name" in the buffer ready for next input operation. Trying to get an integer from there will fail and scanf returns 0 to indicate that failure.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜