Jump to content

Access Violation Reading Loaction 0x00000000 in C

FaiL___

Hi, I'm meant to be basically making a calculator, but for some reason, it keeps saying this error message. The code is breaking on this line: 

if (strcmp((operatorSymbol, "*") == 0))

Thanks in advance for helping, if you could a) fix it, b) explain to me why it is doing this so I can learn, that'd be great. TY

There are 10 types of people in the world. Those that understand binary and those that don't.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Nicholatian said:

Show us the full code. AFAICS operatorSymbol is undefined, which means it shouldn’t even compile.

Full code is: 

#include <stdio.h>

#pragma warning(disable: 4996)

int main(int argc, char *argv[])
{
	float numberOne = 0, numberTwo = 0, answer = 0;
	char operatorSymbol[1];
	int numberOfValuesRead = 0;


	if (argc == 4)
	{
		sscanf(argv[2], "%s", &operatorSymbol);
		numberOfValuesRead = sscanf(argv[1], "%f", &numberOne);
		if (numberOfValuesRead == 1)
		{
			numberOfValuesRead = sscanf(argv[3], "%f", &numberTwo);
			if (numberOfValuesRead == 1)
			{
				if (strcmp((operatorSymbol, "*") == 0))
				{
					answer = numberOne * numberTwo;
					printf("%f", answer);
				}
				else if (strcmp((operatorSymbol, "/") == 0))
				{
					if (numberTwo == 0)
					{
						printf("invalid output");
					}
					else
					{
						answer = numberOne / numberTwo;
						printf("%f", answer);
					}
				}
				else if (strcmp((operatorSymbol, "+") == 0))
				{
					answer = numberOne + numberTwo;
					printf("%f", answer);
				}
				else if (strcmp((operatorSymbol, "-") == 0))
				{
					answer = numberOne - numberTwo;
					printf("%f", answer);
				}
			}
			else
			{
				printf("invalid input");
			}
		}
		else
		{
			printf("invalid input");
		}

	}
	getchar();
}

 

There are 10 types of people in the world. Those that understand binary and those that don't.

Link to comment
Share on other sites

Link to post
Share on other sites

53 minutes ago, FaiL___ said:

Hi, I'm meant to be basically making a calculator, but for some reason, it keeps saying this error message. The code is breaking on this line: 


if (strcmp((operatorSymbol, "*") == 0))

Thanks in advance for helping, if you could a) fix it, b) explain to me why it is doing this so I can learn, that'd be great. TY

You're not calling 'strcmp' the way you think you're calling it, this is the declaration for 'strcmp':

int strcmp ( const char * str1, const char * str2 );

It wants 2 const char pointers as parameters.

 

What are you calling ?

strcmp(       (operatorSymbol, "*") == 0          )

Which decomposes to:

strcmp(   "*" == 0     )

 

Which will always result in:

strcmp(    0    )

So, you're calling:

int strcmp ( int );

A function that was not declared. If you turn up your compiler warnings (or read them in the first place) you should've received a warning along the lines of "implicit declaration of function".

 

Older C standards allowed to call functions that were not declared yet, resulting in a implicit declaration. However the implicitly declared 'strcmp' does not match the real 'strcmp', the parameters don't match at all, and thus you invoke undefined behavior.

Link to comment
Share on other sites

Link to post
Share on other sites

On ‎10‎/‎9‎/‎2017 at 10:29 AM, FaiL___ said:

Hi, I'm meant to be basically making a calculator, but for some reason, it keeps saying this error message. The code is breaking on this line: 


if (strcmp((operatorSymbol, "*") == 0))

Thanks in advance for helping, if you could a) fix it, b) explain to me why it is doing this so I can learn, that'd be great. TY

if strcmp ( ( operatorsymbol, "*") == 0)

note spaces as space gives system place to place + or - for indicating if neg # or pos #

if space then pos

if - then neg

space has to be there ( #) or (-#)

if ( used then followed by space or - then # then closed )

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×