Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

c - Access violation writing location 0x00000000. reading int from keyboard

I am trying to read the input from a keyboard which i will use to create a set of multiplications. If i hardcode the integer to use then the program works fine however when i let the user enter their own number the program crashes and shows an error about an access violation.

I'm sure this is something simple but as I am fairly new to C i'm not entirely sure of all the principles to follow when using the language.

#include <stdio.h>
#include <string.h>
#include <math.h>

void main()
{
    int multiple = 0;
    int i;
    int answer;

    printf("Enter the multiple you wish to use...");
    scanf("%d", multiple);

    printf("The multiplication table for %d is", multiple);

    for(i = 1; i <= 10; i++)
    {
        answer = i * multiple;

        printf("%d X %d = %d",i,multiple,answer);
        printf("
");
    }

    printf("Process completed.");
}

Note: I set the initial value of multiple to 0 otherwise i encounter an error when trying to use an uninitialised value.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
scanf("%d", multiple);

should be:

scanf("%d", & multiple);

In other words, you need to give scanf a pointer to the thing you want to read into. This is a classic mistake in the use of scanf(), and one everyone makes from time to time, so remember this for the next time you make it :-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...