how to pass pointers as parameters in a function? [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to print the sum and diffrence of numbers using pointers but i am getting only sum as output.
#include <stdio.h>
#include<math.h>
void update(int *a,int *b)
{
int sum,sub;
sum = *a + *b;
printf("",sum);
sub = abs(*a - *b);
printf("",sub);
}
int main()
{
int a, b;
int *pa = &a, *pb = &b;
scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}

If I have understood correctly (taking into account the function name) you mean something like the following
#include <stdio.h>
#include <stdlib.h>
void update( int *a, int *b )
{
*a += *b;
*b = abs( *a - *b - *b );
}
int main(void)
{
int a, b;
scanf( "%d%d", &a, &b );
update( &a, &b );
printf( "a = %d, b = %d\n", a, b );
return 0;
}
If for example to enter two values 20 and 10 then the output will look like
a = 30, b = 10
Pay attention to that these calls of printf in your function
printf("",sum);
printf("",sub);
do not make sense.

Related

My program returns 4 instead of expected value 40 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
#include <stdio.h>
int value(int *a);
int main(){
int num = 4;
value(&num);
printf("value of number is = %d", num);
return 0;
}
int value(int *a){
int c = (*a)*10;
return c;
}
In this code, I transfer the address in function but it does not change, Why?
You have 2 ways of having a function change data outside it: you can pass a variable by reference and update it, or you can return a value and use that. You are mixing half of each method instead. I reordered the code to make my commentary clearer.
#include <stdio.h>
int value(int *a);
// here you are passing by reference, good
int value(int *a){
int c = (*a)*10; // but you don't change a
return c; // instead you return a new value
}
int main(){
int num = 4;
value(&num); // but here you ignore the new value.
printf("value of number is = %d", num);
return 0;
}
to make reference way work, you need:
#include <stdio.h>
void value(int *a);
int main(){
int num = 4;
value(&num);
printf("value of number is = %d", num);
return 0;
}
void value(int *a){
*a = (*a)*10; // change the value that a points at
// no need to return anything
}
or to make the return way work:
#include <stdio.h>
// no need to pass by reference
int value(int a);
int main(){
int num = 4;
num = value(num); // use value return by function
printf("value of number is = %d", num);
return 0;
}
int value(int a){
int c = a * 10;
return c;
}

How come does the c program print the number "2" please help explained? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
#include <stdio.h>
int fun1(int a, int b) {
a = a - 3;
b = a / 2;
return b;
}
int main() {
int a = 10;
int b = 17;
int c = fun1(b, a);
int d = fun1(c, b);
printf("%d", d);
}
the code runs I am just trying to figure out what it prints out and why.
In this function
int fun1(int a, int b){
a= a-3;
b=a/2;
return b;
}
the value of the parameter b is not used. So in fact the function may be rewritten like
int fun1(int a, int b){
a = a-3;
return a / 2;
}
or
int fun1(int a, int b){
return ( a - 3 ) / 2;
}
(though the compiler can issue a message that the parameter b is not used,)
In the first call
int c=fun1(b,a);
the parameter a of the function is initialized by the argument b that has the value 17
int b = 17;
So the return value of the function is calculated like ( 17 - 3 ) / 2 and is equal to 7.
In the second call of the function
int d=fun1(c,b);
the function parameter a is initialized by the argument c that has the value 7 due to the previous call of the function.
So the returned value of the function is calculated like ( 7 - 3 ) / 2 and is equal to 2. This value is assigned to the variable d that is outputted.

Trouble dereferencing pointer in c [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am a bit of a sports better, and I am finding myself manually computing the decimal odds from the moneyline odds commonly found on many sportsbooks. I am trying to write a program that will at least remove the burden of calculating this over and over again.
My program is one that will take the inputs "team-one moneyline-teamone team-two moneyline-teamtwo" and output the team name with the decimal odds for a back and lay bet as below:
Input: Rams +230 Bucs -280
I have tried messing around with all of the permutations of dereferencing but can't seem to stop it from giving a segmentation fault.
#include <stdio.h>
#include <stdlib.h>
void calcML(int a, float *b, float *c);
int main (int argc, char* argv[])
{
int oneML, twoML;
float oneDF, oneDA, twoDF, twoDA;
oneML = atoi(argv[2]);
twoML = atoi(argv[4]);
printf("Team one is %s with a ML of %i.\nTeam two is %s with a moneyline of %i.\n", argv[1], oneML, argv[3], twoML);
calcML(oneML, &oneDF, &oneDA);
calcML(twoML, &twoDF, &twoDA);
printf("%s %f %f\n", argv[1], oneDF, twoDA);
printf("%s %f %f\n", argv[3], twoDF, oneDA);
}
void calcML (int a, float *b, float *c)
{
if (a < 0){
int tmp = -a;
*b = 1 + tmp/100;
*c = 100/tmp;
}
else {
*b = 1 + a/100;
*c = 100/a;
}
}
As above, I would expect the output to be:
Team one is Rams with a ML of +230.
Team two is Bucs with a ML of -280.
Rams 3.30 3.80
Bucs 1.36 1.43
Right now the output is:
Team one is Rams with a ML of +230.
Team two is Bucs with a ML of -280.
Segmentation fault
Like I said, I know it is a pointer arithmetic issue, but I have tried a million different permutations and can't figure it out. Thank you to any kind soul that doesn't mind pointing out my dumb mistake!
I was unable to reproduce the segfault, but as someone else has mentioned you appear to be accidentally truncating any floating point values. In calcML, when you execute 1 + tmp/100; the compiler is "helping you" by coercing tmp to an integer. This was using clang 10.0.1 on a MacBook. I would imagine that changing 1 to 1.0 and 100 to 100.0 will help clarify some of the math. Your pointers at least all look correct from what I can tell.
This is the completed code after fixing the float and some of the arithmetic.
#include <stdio.h>
#include <stdlib.h>
void calcML(int a, float *b, float *c);
int main (int argc, char* argv[])
{
int oneML, twoML;
float oneDF, oneDA, twoDF, twoDA;
oneML = atoi(argv[2]);
twoML = atoi(argv[4]);
printf("Team one is %s with a moneyline of %i.\nTeam two is %s with a moneyline of %i.\n", argv[1], oneML, argv[3], twoML);
calcML(oneML, &oneDF, &twoDA);
calcML(twoML, &twoDF, &oneDA);
printf("%s %f %f\n", argv[1], oneDF, oneDA);
printf("%s %f %f\n", argv[3], twoDF, twoDA);
}
void calcML (int a, float *b, float *c)
{
if (a < 100){
float tmp = -a;
*b = 1.0 + 100.0/tmp;
*c = tmp/100.0 + 1;
}
else {
*b = 1.0 + a/100.0;
*c = 100.0/a + 1;
}
}

C: Evaluation of pointer arihmetic: When is an operation done? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
void add( int a, int b) {
a += b;
}
void sub( int *a, int* b) {
*a -= *b; }
void mul( int a, int *b) {
a *= *b;
}
void div( int *a, int b) {
*a /= b; }
int a = 2, b = 3;
sub( &a, &a );
add( a, b );
div( &a, b );
mul( b, &a );
div( &b, b );
add( b, a );
printf( "%d\n", a );
printf( "%d\n", b );
Why isn't a = 1 and b = 2, instead a = 0 and b = 1.
Can anyone explain to me what *, &, so what pointer, in this code is the reason for doing an operation?
In general this has nothing to do with pointer arithmetic. It's about calling the function correctly, i.e. when to use a pointer and when to use an integer.
Since you want to change a and leave b unchanged, all your functions shall have the form:
void function(int* const a, const int b) {…}
Since a is "passed as a pointer" you can change the value of a using the syntax *a = …. Since the pointer is const the pointer itself can't be changed. As b is never to be changed, it's best to pass it as a const.
If you change all function in that way, correct the function body accordingly and also the way you call the functions, you'll be fine.
example
void add( int* const a, const int b) {
*a += b;
}
int a = 0;
add(&a, 42);

Value assigned to s variable when uses %d instead %f [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I assume output to be 8 but the line s=a+b assign value 5 to s variable not sum of a+b. I know that I am using %d instead of %f.
#include <stdio.h>
void summ(int, int);
int main(){
int a = 3, b = 5;
summ(a, b);
return 0;
}
void summ(int a, int b){
float s;
s = a + b;
printf("%d", s);
}
You can't use any old format specifier in a printf(). It always has to match the type of the variable you are passing in. If you want to print a value as another type, you have to convert it first, then print the converted value. So either this has to be:
void summ(int a, int b)
{
float s;
s = a + b;
printf( "%f\n", s );
}
or
void summ(int a, int b)
{
int s;
s = a + b;
printf( "%d\n", s );
}
In the case of your sum function, you won't see a noticeable difference (except that %d does not print a ".0000" after the number, but you could have suppressed that by adding a length of 0 to the fraction of your %f by writing it as %.0f).
OTOH, if you have a division:
void divv(int a, int b)
{
float s;
s = a / b;
printf( "%f\n", s );
}
the CPU will perform a (often faster) integer division. That means that 3 / 5 (which would be 0.6) just gives you 0 because it didn't bother calculating the fraction. The compiler doesn't look at the type of variable an expression is assigned to, so it doesn't see that it could actually store the fraction. It just looks at the operands of the operator, which are both int, calculates an int, and expands it into a float. So if you wanted a precise result, you'd have to do:
void divv(int a, int b)
{
float s;
s = (float)a / (float)b;
printf( "%f\n", s );
}
because as soon as one of the arguments is a float, it will perform a floating point division and also calculate the fraction.
here is a possible version of the code that cleanly compiles and performs the desired functionality.
#include <stdio.h>
void summ(int, int);
int main( void )
{
int a = 3;
int b = 5;
summ(a, b);
return 0;
} // end function: main
void summ(int a, int b)
{
float s;
s = (float)(a + b);
//printf("%d", s);
printf( "%f\n", s );
} // end function: summ
The output from the above code is:
8.000000

Resources