Assign a value to the pointer and multiply (directly)

5

How do I multiply the pointer as soon as I assume a value to it?

#include <stdlib.h>

#include <stdio.h>


int main (void) {

    int a,*b,*c,*d;

    printf ("\nInforme um valor: ");
    scanf ("%i",&a);

    b = &a; // Como fazer a multiplicação aqui, logo que eu assumo o valor do ponteiro ?
    c = &a;
    d = &a;

    printf("\n%i\n%i\n%i\n",*b*2,*c*3,*d*4); // Assim a legibilidade do código vai ficar ruim com muitos valores

    return 0;
}
    
asked by anonymous 29.06.2016 / 02:23

2 answers

4

The variables b , c and d are pointer types, so there will be memory addresses. You do not want to multiply the address, only the value. The first operation is to initialize the variable with a pointer somewhere.

What you can do is create 3 auxiliary variables with multiplied values and point each of these code variables to these auxiliaries. Much worse, right?

You can after you initialize by pointing to the same variable it creates another 3 helpers to store the multiplication in them. It does not seem necessary.

It seems like a simple exercise and it is correct. If not this would need to have more details.

    
29.06.2016 / 02:36
1

&a is not a value - it is the address where the value that was typed in the scanf is - 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 in that memory location - a single number for the variables "a", "* b", "* c" and "* d".

That is, even if you do it on 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.

You can not tell 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 get independent memory addresses for them, so they can contain different numbers.

In addition - note that in this situation of the question, you have in fact two distinct attributes to assign: one is the memory address where the contents of your pointer variables will be (you always try to reuse the same address & a) - and another is the numeric value you want to put in that final address (the value of "a" multiplied by different numbers). If you need to set two distinct values, you need to do two assignments.

Now, for example, if you declare a vector, instead of pointer variables with different names, you'll be in a situation where you already have the addresses created and defined by the compiler, and you just 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;
}

Line for(int i=0, *c=b; i<=3; i++) *c = a * (i + 1); initializes for with variable i as usual - only a counter, but also a c pointer that points to the first element of the b vector. When i is incremented, the c position is also (The language C is smart for pointers - c++ in case it will not forward a byte, but the size of the data type int bytes - and point right to the next element of the b vector).

    
29.06.2016 / 18:11