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

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

Related

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.

How do I print an actual number from this program that contains a function that returns a pointer?

#include <stdio.h>
int *max(int *, int *);
int main()
{
int *p, i, j;
p = max(&i, &j);
printf("%d\n", i);
return 0;
}
int *max(int *a, int *b)
{
if(*a > *b)
return a;
else
return b;
}
This is a program intended to return an integer that is bigger. A function "max" returns a pointer, as you can see. I want to print an actual integer here, but I'm stuck and cannot find a proper way to accomplish it. Can somebody help or give some hint to solve my problem?
Also, I would love to know that why there should be an asterisk in front of the function "max". Should there always be an asterisk when a function returns a pointer? The book that I am currently studying lacks information about this specific part, so can someone scratch my back? ;)
Last question first - max returns the value of either a or b. Since both a and b have type int * (pointer to int), then the return type of max also needs to be int *.
To access the integer value, you would need to dereference the result of max:
int main()
{
int *p, i, j;
/**
* The values of i and j are indeterminate at this point;
* you need to assign valid values to them before calling
* max.
*/
i = some_value();
j = some_other_value();
p = max(&i, &j);
printf("%d\n", *p); // Dereference p here to print the int value
return 0;
}
Another way to look at it is that the expressions *a, *b, *p, and *max( &i, &j ) all have type int.
If you want max to return an int rather than an int *, then you will need to dereference a and b in the return statements:
int max( int *a, int *b )
{
if ( *a > *b )
return *a;
else
return *b;
}
Although...
It's not clear why you're passing pointers as arguments to max; you're not attempting to modify the values of a or b, so there's really no need to use pointers at all. Just define max as
int max( int a, int b )
{
if ( a > b )
return a;
return b;
}
and call it as
int m = max( i, j );
or even
printf( "max( %d, %d ) = %d\n", i, j, max( i, j ) );

how to pass pointers as parameters in a function? [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 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.

Write a program that swaps values within an array [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 5 years ago.
Improve this question
This is for homework
Here is my program
#include <stdio.h>
void swap (int *a , int *b , int *c , int *d , int *e ) {
int temp1 = a;
a = b;
b = temp1;
int temp2 = b;
c = d;
d = temp2;
int temp3 = e
e = a;
a = temp3;
}
void swap (int *a , int *b , int *c , int *d , int *e );
int main(void)
{
int arr[15] = {0};
int i;
int a = arr[0], b = arr[7], c = arr[8], d = arr[3], e = arr[14];
printf("Enter 15 integers\n");
for(i = 0; i < 15; ++i) {
scanf("%d" , &arr[i]);
//printf("The ints are %d " , arr[i]); checked if all the ints were recorded
}
swap(a, b, c, d, e);
printf("Swapped array:\n %d" , arr[i]);
return 0;
}
So The program is asking me to have the user enter 15 integers. The function would have to swap the 1st integer entered (arr[0]) with the seventh integer entered (arr[7]). Then swap the 8th with the 3rd, then swap the last one with the first. My program complies but gives a handful of warnings and when I try to print my swapped array, all I get is a value of 0. Any help would be appreciated!
Edit
Sorry I heard from someone to not worry about the warnings- counter-intuitive to listen. The warnings are:
[In the function]
assignment makes pointer from integer without a cast
initialization makes integer from pointer without a cast
In main
passing argument [numbers] of swap makes pointer from integer without a cast
There's several problems with your code.
First, the swap function doesn't work. You're swapping the values of the pointers that are local variables inside the function, you're not swapping the array elements they point to. You need to indirect through pointers to get the values.
int temp1 = *a;
*a = *b;
*b = temp1;
And similar for all the other pairs you're swapping.
Second, swap expects pointers as arguments, but you're passing int variables from main(). You need to use pointer variables, which you set to the addresses of the array elements:
int *a = &arr[0], *b = &arr[7], *c = &arr[8], *d = &arr[3], *e = &arr[14];
You might consider changing the swap() function so it just takes two pointers, and call it 3 times to do all 3 swaps.
Finally, to print the array, you need to print the elements in a loop:
printf("Swapped array: ");
for (i = 0; i < 15; i++) {
printf("%d ", arr[i]);
}
printf("\n");

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