2

I remember my programming prof said that multiplication and division of pointers are not allowed. We have a seatwork that needs us to create a program that adds, subtracts, multiplies and divides two numbers using pointers.

This is my code in the main function:

float num1, num2, a, b, c, d;

printf("Enter a number: ");
scanf("%f", &num1);
printf("Enter another number: ");
scanf("%f", &num2);

a = add(&num1, &num2);
b = subtract(&num1, &num2);
c = multiply(&num1, &num2);
d = divide(&num1, &num2);

printf("Sum: %.2f\nDifference: %.2f\nProduct: %.2f\nQuotient: %.2f", a, b, c, d);
getch();
return 0;

This is my code for the add, subtract, multiply, and divide functions:

float add(float *x, float *y)
{
    return *x+*y;
}
float subtract(float *x, float *y)
{
    return *x-*y;
}
float multiply(float *x, float *y)
{
    return *x * *y;
}
float divide(float *x, float *y)
{
    return *x / *y;
}

My code runs and works but is it allowed?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
zaira
  • 29
  • 1
  • 1
  • 2
  • 6
    yes it is allowed, `*x` is not a pointer but a value pointed by `x`. and hence `*x+*y;` is addition of two values pointed by `x` and `y`. – TruthSeeker Jan 25 '20 at 06:51
  • Your code is fine, but it never multiplies or divides a pointer. It multiplies or divides the pointed at values, and the pointed at values are not pointers, so multiplication and division is fine. You can't add pointers, either; you can only subtract pointers. – Jonathan Leffler Jan 25 '20 at 07:13
  • Think about what would happen. `float arr[] = {1., 2., 3., 4., 5.}, *p = arr, *p2 = arr + 2, *p3 = p / p2;` (or `float *p3 = p2 / p;` ) So what you have (on a 64-bit computer) is either `(64-bit address - 2) / 64-bit address` or `(64-bit address + 2) / 64-bit address`. In the first case you will have a number less than `1`, in the second case, a number slightly more than `1`. Both resulting addresses are likely at the bottom of the system reserved memory space (regardless you won't have valid access to the resulting address). It's just not allowed. – David C. Rankin Jan 25 '20 at 08:00
  • Thank you! I'm still confuse what pointers are and how they actually work. Now I know the exception only applies to pointers itself. – zaira Jan 25 '20 at 08:32
  • A pointer is simply a normal variable that holds the *address of* something else as its value. In other words, a pointer *points to* the memory address where something else can be found. For example, while `int a = 5;` stores the immediate value `5` as its value, `int *b;` creates a pointer to `int`, and `b = &a;` stores the address of `a` as `b`'s value (the memory address where `5` is currently stored). If you need the value stored at the memory address held by a pointer, you *dereference* the pointer using the unary `'*'` operator, e.g. `int c = *b;`. – David C. Rankin Jan 25 '20 at 13:42
  • You may benefit from this answer [C intro - How to pass a parameter by reference in function?](https://stackoverflow.com/questions/58888981/c-intro-how-to-pass-a-parameter-by-reference-in-function/58889363?r=SearchResults&s=2|0.0000#58889363) – David C. Rankin Jan 25 '20 at 13:45
  • Pointers are confusing, but also important, because they introduce a *level of indirection*, which is a powerful and useful concept, but which is hard to think abut it until you've learned about and are used to thinking about it. – Steve Summit Jul 01 '21 at 17:13
  • For an ordinary value like `5`, you have one thing to think about: the value 5. For an ordinary variable like `int a = 5`, you have two things to think about: the (name of the) variable is `a`, and the value "stored in" the variable is 5. But for a pointer variable like `int *ip = &a`, you now have *three* things to think about: its name, its value -- which for a pointer variable is where it points -- *and* the value at whatever location it is that it points to. `ip`'s value is "pointer to location `a`", and the pointed-to value is 5. – Steve Summit Jul 01 '21 at 17:16

2 Answers2

7

Multiplication and division of pointers are not allowed in C.

For example,

int *ptr1, *ptr2, *ptr3;

ptr3 = ptr1 * ptr2; // Error: Multiplication of pointers

ptr3 = ptr1 / ptr2; // Error: Division of pointers

This discussion is worthwhile to know the reasons behind this restriction on pointers in C.

Getting into your code, it works because you are not multiplying or dividing any pointers but multiplying and dividing the values pointed by those pointers as you have used the dereference operator.

For example,

int a = 1, b = 2, c = 3;

int *ptr1 = &a;

int *ptr2 = &b;

int *ptr3 = &c;

*ptr3 = *ptr1 * *ptr2; // No error: c = a * b

*ptr3 = *ptr1 / *ptr2; // No error: c = a / b

See: meaning of "referencing" and "dereferencing"

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
4

*x and *y refer to the values pointed by them, not the pointers.

*x * *y -> allowed.

x * y -> not allowed.

dev7060
  • 120
  • 8