Segmentation Error in C programming - c

I am trying to compile a program in C, but I keep getting the following error:
Segmentation fault
Here is the code:
#include <stdio.h>
#define calculation1 main
void calculation1(int *num1, int *num2) {
int total;
printf("Program One: \n");
printf("=========== \n");
printf("Number One: ");
scanf("%d", &num1);
printf("Number Two: ");
scanf("%d", &num2);
total = *num1 + *num2;
printf("%d + %d = %d \n",&num1,&num2,total );
}
What am I doing wrong here? How can I fix this error?

What am I doing wrong here? How can I fix this error?
Problem 1
By using
#define calculation1 main
void calculation1(int *num1, int *num2) {
you are essentially using:
void main(int *num1, int *num2) {
This is wrong. main needs to be:
int main(void) {
or
int main(int argc, char** argv) {
Your program is subject to undefined behavior.
Problem 1
You are using
scanf("%d", &num1);
scanf("%d", &num2);
when num1 and num2 are of type int*. Thy need to be:
scanf("%d", num1);
scanf("%d", num2);
Problem 3
You are using
printf("%d + %d = %d \n",&num1,&num2,total );
Given the type of num1 and num2, that needs to be:
printf("%d + %d = %d \n", *num1, *num2, total );
Fix
Your program needs a bit of an overhaul. Try:
#include <stdio.h>
#define calculation1 main
int calculation1() {
int num1; // Not int*. If you use int*, you'll need to allocate memory
int num2;
int total;
printf("Program One: \n");
printf("=========== \n");
printf("Number One: ");
scanf("%d", &num1); // You need to use &num1 since num1 is of type int.
printf("Number Two: ");
scanf("%d", &num2);
total = num1 + num2;
printf("%d + %d = %d \n", num1, num2, total);
}

scanf("%d", num1);
scanf("%d", num2);
scanf needs an address and num1 and num2 already contain the address as you pass them as pointers in the function.
The other thing to change is:
printf("%d + %d = %d \n",*num1,*num2,total );
*num1 dereferences a pointer to provide the value

Despite errors pointed by #jayant-
#define calculation1 main
void calculation1(int *num1, int *num2) {
So , calculation1 will be replaced by main ,so in short this is your main function .
This is not a valid and should avoid it any case . I am confused how you make a call or take command line argument ? But this is definitely incorrect .
Simply,do this -
int main(void) or int main(int argc,char *argv[]) and declare num1 and num2 as int variables and then take input in it and perform desired operation.

Related

Pointer variable not being updated for C

#include <stdio.h>
void gcdFinder(int num1, int num2, int *result);
int main()
{
int n1, n2, result;
printf("Enter 2 numbers: \n");
scanf("%d %d", &n1, &n2);
gcdFinder(n1, n2, &result);
printf("rGcd2(): %d\n", result);
return 0;
}
void gcdFinder(int num1, int num2, int *result)
{
printf("Initial : %d %d \n",num1,num2);
*result=num1;
if(num2 ==0 ){
return;
}else{
gcdFinder(num2,(num1%num2),&result);
}
}
I am trying to find the GCD the 2 inputs and storing the result into the result variable. However my code does not seem to work as it only stores one time.
As the output suggest my final value for num1 should be 1 however it is not stored and it remains as 4.
In the function gcdFinder change
gcdFinder(num2,(num1%num2),&result);
to
gcdFinder(num2,(num1%num2), result);
^
No & as result is already a pointer here
Notice:
In main it's correct to use &result because result in main is an int

Issue with swapping program, the program crashes

I am trying to make a program that scans 3 ints and then passes them through a function that sort them by this way- the biggest number will be at 'num3', the second will be at 'num2' and the lowest one will be at 'num1' but for some reason the program crashes when it gets to the sort function.
#include <stdio.h>
#include <stdlib.h>
void swap(int* a, int* b);
void changer(int* num1, int* num2, int* num3);
int main()
{
int num1 = 0;
int num2 = 0;
int num3 = 0;
printf("Please enter your value for 'num1': ");
scanf("%d", &num1);
getchar();
printf("Please enter your value for 'num2': ");
scanf("%d", &num2);
getchar();
printf("Please enter your value for 'num3': ");
scanf("%d", &num3);
printf("\nYour nums before- \n");
printf("num1 == %d\n", num1);
printf("num2 == %d\n", num2);
printf("num3 == %d\n", num3);
changer(&num1, &num2, &num3);
printf("\nYour nums after- \n");
printf("num1 == %d\n", num1);
printf("num2 == %d\n", num2);
printf("num3 == %d\n", num3);
system("PAUSE");
return 0;
}
void changer(int* num1, int* num2, int* num3)
{
if (*num1 > *num3)
{
swap(*num3, *num1);
}
else if (*num1 > *num2)
{
swap(*num1, *num2);
}
if (*num2 > *num3)
{
swap(*num3, *num2);
}
}
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
You should not dereference the pointers when you pass them to swap() since it takes int * and you would be passing int thus causing undefined behavior and in your case a crash. The compiler should warn about incompatible parameters.
Change every occurrence of
swap(*num1, *num2);
/* ^ ^ remove these */
with
swap(num1, num2);
swap(int*, int*) expects the integer pointer to be passed in, assume the value of num1 = 0 and num2 = 2 and when you pass these values to swap(), by dereferencing you are invoking like this swap(0, 2) or on 32 bit system
swap(0x00000000, 0x00000002)
Inside swap(int* a, int* b) you dereference the memory location 0x00000000 (not the memory pointed to by a) at the line,
int temp = *a;
and then you dereference 0x00000002 b at this line and trying to assign it to 0x00000000 (by dereferencing *a)
*a = *b;
You are generating
Memory access violation condition.
which is causing the crash. Summarizing, please do not ignore compiler warnings. Compiler raises warning on the line where you are making swap() calls.
'int *' differs in levels of indirection from 'int'
Both functions take integer pointers (i.e., memory addresses of integers) as arguments. So, the values of the arguments a, b, num1, num2, and num3 must ALWAYS be memory addresses of integers. In the function changer, values of the variables num1, num2, and num3 are ALREADY memory addresses. When you write *num1, value of *num1 is integer but not the memory address of an integer. So, you need to call the function swap as follows:
swap(num3, num1);
swap(num1, num2);
swap(num3, num2);

Return a value to main function with pointers

Is there any other way to return a value to main function using void functions. I don't want to use the following way because I want to ask user to enters some values in main function and then check if those values are correct in the void function.
#include <stdio.h>
void fun_1(int *num1, int *num2) {
printf("Enter both numbers: ");
scanf("%d", num1);
scanf("%d", num2);
}
int main(void) {
int num1, num2, total;
fun_1(&num1, &num2);
total = num1 + num2;
printf("%d \n", total);
}
In this code we ask user to enter values in void function, but I would like to ask user to enter values in main!
There is another way to make use of global variables, but as your program grows more complex, it will probably create more hassles for you then it is worth.
If you use global variables to store your inputs inside the sub-function, then you can directly access them in your main() function to get the values. You won't be needing a return from your sub-function.
Having said that, it would be better to change the function return type (maybe returning a struct containing the user-supplied values, if you more than one value to be returned) to match your requirements.
If you want to read the values in main but check them (ex. make sure both are positive) in another function, you can do this:
#include <stdio.h>
void check_numbers(int num1, int num2, int *valid) {
if ((num1 > 0) && (num2 > 0)) {
*valid = 1;
} else {
*valid = 0;
}
}
int main(void) {
int num1, num2, total, valid;
printf("Enter both numbers: ");
scanf("%d", num1);
scanf("%d", num2);
check_numbers(num1, num2, &valid);
if (valid) {
total = num1 + num2;
printf("%d \n", total);
} else {
printf("numbers are invalid\n);
}
}

c programming error with binary operators

Im trying to write a calculator program. i have wrote the first part of it but i keep get the same error: invalid operands of types unsigned int*' andchar[80]' to binary `operator&'
Please help me
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
unsigned int num1, num2, num3;
char s[80];
int main (){
printf("type in an expression: ");
scanf(" %x %s %x\n", &num1 &s &num2);
if(strcmp ("add", s) == 0){
num3 = num1 + num2;
}
if(strcmp("subtract", s) == 0){
num3 = num2 - num1;
}
printf("the answer is: %x", num3);
}
try:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
unsigned int num1, num2, num3;
char s[80];
int main (){
printf("type in an expression: ");
scanf(" %x %s %x", &num1, s, &num2);
if(strcmp ("add", s) == 0){
num3 = num1 + num2;
}
if(strcmp("subtract", s) == 0){
num3 = num2 - num1;
}
printf("the answer is: %x\n", num3);
system("pause");
}
note: notice that I remove \n in the scanf..
As Yohanes mentions, you need to have the commas in between the arguments in the scanf, otherwise the compiler is trying to do: get the address of num1 (&num1) and logically AND it with the address of the array s (address is implied here because it is an array) and logically AND that with the value contained in num2.
I would suggest that you add an else between the two if statements since they are mutually exclusive.
Furthermore, you probably want to add a \n to the printf statement
printf("the answer is: %x\n", num3);
to flush the output.

Math functions in C

I am using Code::blocks to compile my first multiple source file, learned from "C Programming in easy steps" by Mike McGrath. Unfortunately my math functions seem to be having issues. Here's the header that contains the functions:
/* this header file contains utility functions */
int square(int x); /* function prototypes */
int multiply(int x, int y);
int square(int x)
{
return (x*x);
}
int multiply(int x, int y)
{
return (x*y);
}
The only function having the problem is "square()". It reads the input of "2" as "2293356" and outputs the square as "553755367"... What the heck?!?
Here's the menu.c file... There's menu.c, ops.c, calc.c, and utils.h. Abaov is the .h.
MENU.c
include
void menu();
void menu()
{
int num;
printf("\n\tEnter the number of an operation:\n");
printf("\t1. Square a number\n");
printf("\t2. Multiply two numbers\n");
printf("\t3. Exit\n");
scanf("%d", &num);
switch(num)
{
case 1 : getnum(); break;
case 2 : getnums(); break;
case 3 : return;
}
}
Here's ops.c...
#include <stdio.h>
#include "utils.h"
void getnum();
void getnums();
void getnum()
{
int num;
printf("Enter an integer to be squared: ");
scanf("%d", &num);
printf("%d squared is %d\n, num, square(num)");
menu();
}
void getnums()
{
int num1, num2;
printf("Enter two numbers to be multiplied, ");
printf("seperated by a space: ");
scanf("%d", &num1);
scanf("%d", &num2);
printf("%dx%d = %d\n", num1, num2, multiply(num1, num2));
menu();
}
This is the last part of the program, calc.c,
#include <stdio.h>
int main()
{
menu();
printf("end\n");
return 0;
}
The square of 2293356 doesn't fit into int and therefore overflows which leads to undefined behavior! As to why it reads 2 as 2293356 cannot be answered without more code.
Update:
And here's your real error:
printf("%d squared is %d\n, num, square(num)");
should be
printf("%d squared is %d\n", num, square(num));
:)

Resources