pointers functions

1
2
3
unsigned short int multiplication ( unsigned short int*x, unsigned short int*y ) {
return *x * *y; // how to write this line properly? it is printing garbage value
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
unsigned short int addition ( unsigned short int*, unsigned short int* );
unsigned short int subtraction ( unsigned short int*, unsigned short int* );
unsigned short int multiplication ( unsigned short int*, unsigned short int* );
unsigned short int divison ( unsigned short int*, unsigned short int* );
int main() {
unsigned short int a = 0;
unsigned short int b = 0;
std::cin >> a >> b;
std::cout << addition(&a,&b) << std::endl;
std::cout << subtraction(&a,&b) << std::endl;
std::cout << multiplication(&a,&b) << std::endl;
std::cout << divison(&a,&b) << std::endl;
}
unsigned short int addition ( unsigned short int *x, unsigned short int *y ) {
return *x + *y;
}
unsigned short int subtraction ( unsigned short int* x, unsigned short int* y ) {
return *x - *y;
}
unsigned short int multiplication ( unsigned short int*x, unsigned short int*y ) {
return *x * *y;
}
unsigned short int divison ( unsigned short int*x, unsigned short int*y ) {
    if ( *y == 0 )
return *x / *y;
else
    return 0;
}
this is my whole code 
Last edited on
The code is correct. However, you're returning a short int which is typically 16 bits and holds values 0-65535. So if your result would be larger than that, the result will overflow.

The problem might actually be in division(). Line 25 is wrong. You want to return *x / *y if y doesn't equal zero.

Finally, the code would be more efficient if you passed the unsigned int's directly instead of using pointers. In other words like this:
1
2
3
unsigned short int multiplication ( unsigned short int x, unsigned short int y ) {
return x * y;
}
@dhayden
So if your result would be larger than that, the result will overflow.
im aware of using short int, but i only input 1 and 2 when multiplied is 3, but it is printing 65535

The problem might actually be in division(). Line 25 is wrong. You want to return *x / *y if y doesn't equal zero.
oh thanks.

 
Finally, the code would be more efficient if you passed the unsigned int's directly instead of using pointers. In other words like this: 

it still prints garbage values. base on my observation when i input 2nd number that is higher than first number , it is printing 65500+. otherwise it is not.
You're seeing the problem when you subtract a larger number from a smaller number. That of course results in a negative number. However, you are declaring all your values to be unsigned, which means that negative numbers can not be represented.

When you enter 3 and 4, the signed result of subtracting them would be -1. However, since you've declared your values to be unsigned, you get the twos complement equivalent, which is 65535 (all bits set).

Note also that at line 26 you're doing integer arithmetic. 3/4=0.


How do you call your function? For me this works perfect:
1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdio>

unsigned short multiplication(unsigned short *x, unsigned short *y) {
	return *x * *y;
}

int main(){
	unsigned short x = 5, y = 2;
	printf("%hu", multiplication(&x, &y));
	getchar();
	return 0;
}
You're seeing the problem when you subtract a larger number from a smaller number

o yeah, i thought it was on multiplication function output . i was just too dizzy lol
thats all.
thank you :D
i wouldnt post like this if i noticed that lol xD

by the way, im just practicing to use pointeres
am i using the pointers right?
Last edited on
@TtheHardew
i dont see any difference to your code and mine
its just that i declare my functions before defining it
Last edited on
by the way, im just practicing to use pointeres
am i using the pointers right?

Your use of pointers is fine.
thanks
Topic archived. No new replies allowed.