&a
is not a value - is the address where is the value that was typed in the scanf - the same address as its variable "a". Each address can only have one value - so even if some gymnastics you multiply the value that is in &a
this new value multiplied will be the number at that memory position - a single number for the variables "a", "*b", "*c" and "*d".
I mean, even if you do it in more than one line - something like:
...
b = &a;
*b *= 2;
...
When printing
printf("\n%i\n%i\n%i\n",*b,*c,*d);
You will see the same number printed 3 times.
It’s not enough to know what your ultimate goal is, but for this simple case, it’s best to use normal variables, not pointers. If you need pointers, you need to arrange independent memory addresses for them, so they can contain distinct numbers.
In addition - understand that in this question situation, you actually have two distinct things to assign: one is the memory address where the contents of your pointer variables will be (that you always try to reuse the same address &a) - and another is the numerical value you want to put at that final address (the value of "a" multiplied by different numbers).
If you need to set two different values, you need to make two assignments.
Now, for example, if you declare an array, instead of pointer variables with different names, you will be in a situation where you already have the addresses created and defined by the compiler, and only need to assign the values -
#include <stdlib.h>
#include <stdio.h>
int main (void) {
int a;
int b[3];
printf ("\nInforme um valor: ");
scanf ("%i",&a);
for(int i=0, *c=b; i<=3; i++, c++) *c = a * (i + 1);
printf("\n%i\n%i\n%i\n",b[0], b[1], b[2]);
return 0;
}
The line for(int i=0, *c=b; i<=3; i++) *c = a * (i + 1);
initiates the for
coma i
as usual - only a counter, but also a pointer c
pointing to the first element of the vector b
. When i is incremented, the position c
is also (The language C
is smart for pointers - c++
in case will advance not one byte, but the size of the data type int
bytes - and point to the next vector element b
).
What you want to multiply?
– Maniero
In case I need to show double, triple and quadruple the number typed by the user ( this should be done with pointers ) !
– user48471
Sounds right to me. If you have some other goal, you need to ask the question.
– Maniero
Hm, so there’s no way to multiply directly when passing a value to the pointer ?
– user48471
If you want to do something different, you need to demonstrate what it is.
– Maniero
Okay, thanks for your attention ...
– user48471