Pointer variable not being updated for C - 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

Related

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);

My program prints 0 instead of the expected number

So I made a function that returns a value which is the connection (sum) of 2 numbers that the user gives. But everytime I enter these 2 numbers, the result is somehow always 0.
float connection(int num1, int num2, float result)
{
result= num1 + num2;
return (result);
}
int main()
{
int num1= 0;
int num2= 0;
float result= 0.0;
printf("\nChoose the first number you'd like to connect: ");
scanf("%d", &num1);
getchar();
printf("choose the second number you'd like to connect: ");
scanf("%d", &num2);
connection(num1, num2, result);
printf("The result is: %f", result);
}
The problem here is (I believe) that you expect the argument result you pass to the function to change value. That's not how C works, instead all arguments are passed by value and the variable result inside the called function is a local variable which has no connection to the result variable in the calling function. Changing one variable will not affect the other.
Instead use the value the function returns:
result = connection(num1, num2, result);
Of course, this means passing result as an argument is totally meaningless, and you don't even need it in the function:
int connection(int num1, int num2)
{
return num1 + num2;
}
Also note that I changed the return-type, as adding two integers will never result in a floating-point number.
On a side-note to modify the argument means you have to pass it by reference, which is not supported by C. It can be emulated though, by using pointers:
void connection(int num1, int num2, int *result)
{
*result = num1 + num2;
}
Then call it as
int result;
connection(num1, num2, &result);
You are mixing up two possibilities: returning a value and changing a variable by passing it to a method as a reference.
This is how to return a value (in this case, you don't need the result parameter):
float connection(int num1, int num2)
{
float result= num1 + num2;
return result;
}
...
result = connection(num1, num2);
This is how to pass a variable as a reference (in this case you don't need a return value):
void connection(int num1, int num2, float *result)
{
*result= num1 + num2;
}
...
connection(num1, num2, &result);
I think forgot to assign your result back to your variable:
connection(num1, num2, result); // returned value is missed
result = connection(num1, num2, result); // returned value saved
If, instead, you want your function to modify the variable, C is passed by value so you need some pointers:
void connection(int num1, int num2, float *result)
{
*result = num1 + num2;
}
connection(num1, num2, &result);
Simply make your function call pass by reference
#include<stdio.h>
float connection(int *num1, int *num2, float *result)
{
*result= *num1 + *num2;
return (*result);
}
int main()
{
int num1= 0;
int num2= 0;
float result= 0.0;
printf("\nChoose the first number you'd like to connect: ");
scanf("%d", &num1);
getchar();
printf("choose the second number you'd like to connect: ");
scanf("%d", &num2);
connection(&num1, &num2, &result); //Function Called by reference
printf("The result is: %f", result);
}
For further details on function calls Read It

Segmentation Error in C programming

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.

How to get the largest integer using modular programming?

So, I've been trying to get two integers from the user and the program should return the value of the larger one. Here is my sample program:
#include<stdio.h>
int larger(int a, int b);
int main()
{
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("\n%d is larger than the other", larger(num1, num2) );
}
int larger(int a, int b)
{
if (a>b)
{
printf("%d", a);
}
else if(b>a){
printf("%d", b);
}
}
But the problem here is that whenever I compile then run the program an integer value of 1 is placed beside the sentence "is larger than the other" ,while the largest integer (the integer that should be beside the 'is larger than the other') is placed above the integer 1.
SAMPLE OUTPUT:
Enter first number: 5
Enter second number: 3
5
1 is larger than the other
What should I do to place the larger value placed where it should be?
int larger(int a, int b)
Your function doesn't have a return statement . If you want largest value between two you need to return it from function . Like this -
int larger(int a, int b)
{
if (a>b)
{
//printf("%d", a); unnecessary as you print value in main
return a;
}
else if(b>a){
//printf("%d", b);
return b;
}
else
return a; //in this case take care of output message as both variables will be equal
}
larger needs to actually return the larger value: write return a;, and return b; in the appropriate places. And the output will look odd if you retain the printf calls in that function.
Currently your program behaviour is undefined as the return value is missing: the output is currently arbitrary.
You're also not dealing with all possibilities. What should happen if a and b are equal? You must return something on all control paths.
Your function isn't returning anything, you need to enable compiler warnings or alternatively, get a new compiler.
That being said, in case you aren't afraid of boolean logic, the function can simply be written as
static inline bool larger (int a, int b)
{
return a > b;
}
Naturally, you'll have to rewrite main() to check the result:
if(larger (x, y))
{
printf("x larger than y");
}
In int function larger(int a, int b) you should just compare and return the value rather than printing it.
#include<stdio.h>
int larger(int a, int b);
int main()
{
int num1, num2, largeOftheTwo= 0;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
if(num1 != num2)
printf("\n%d is larger than the other", larger(num1, num2));
else
printf("\n%d is equal to %d", num1, num2);
return 0;
}
int larger(int a, int b)
{
if (a>b)
return a;
else
return b;
}

In C: passing and printing variables arguments of a function

I am trying to write a program which uses function with variable number of arguments. An additional task is to print all the arguments of every function call separately. The code is as follows:-
#include<stdio.h>
#include<stdarg.h>
#include<string.h>
int mul(int x,...);
int main()
{
int a=1,b=2,c=3,d=4,x;
printf("The product of %d is :%d\n",a,mul(1,a));
printf("The product of %d, %d is :%d\n",a,b,mul(2,a,b));
printf("The product of %d, %d, %d is :%d\n",a,b,c,mul(3,a,b,c));
printf("The product of %d, %d, %d, %d is :%d\n",a,b,c,d,mul(4,a,b,c,d));
return 0;
}
int mul(int x,...)
{
int i,prod=1;
va_list arglist;
va_start(arglist, x);
for(i=0;i<x;i++)
{
prod*=va_arg(arglist,int);
}
printf("\n");
for(i=0;i<x;i++)
{
printf("The argument is %d\n",va_arg(arglist,int));
}
va_end(arglist);
return prod;
}
The output of this program is as follows:-
The other piece of code is:-
#include<stdio.h>
#include<stdarg.h>
#include<string.h>
int mul(int x,...);
int main()
{
int a=1,b=2,c=3,d=4,x;
printf("The product of %d is :%d\n",a,mul(1,a));
printf("The product of %d, %d is :%d\n",a,b,mul(2,a,b));
printf("The product of %d, %d, %d is :%d\n",a,b,c,mul(3,a,b,c));
printf("The product of %d, %d, %d, %d is :%d\n",a,b,c,d,mul(4,a,b,c,d));
return 0;
}
int mul(int x,...)
{
int i,prod=1;
va_list arglist;
va_start(arglist, x);
for(i=0;i<x;i++)
{
prod*=va_arg(arglist,int);
}
printf("\n");
va_end(arglist);
va_start(arglist,x);
for(i=0;i<x;i++)
{
printf("The argument is %d\n",va_arg(arglist,int));
}
va_end(arglist);
return prod;
}
The output is as follows:-
Why is this difference? Any help?
In the first example you are missing two lines:
va_end(arglist);
va_start(arglist,x);
This means that after doing the multiplication you are reading past the end of the parameters. The values that are displayed are whatever happened to be on the stack.
va_arg(va_list ap, type) retrieves next argument in the argument list.So in the first code you are consuming the arguments after one loop . Instead of 2nd code you can use the following code which prints argument and maintains multiplication in a single loop
int mul(int x,...)
{
int i,m,prod=1;
enter code here
va_list arglist;
enter code here
va_start(arglist, x);
for(i=0;i<x;i++)
{
m=va_arg(arglist,int);
prod*=m
printf("The argument is %d\n",m);
}
printf("\n");
return prod;
}

Resources