Assign a value to the pointer and multiply (directly)

Asked

Viewed 1,808 times

5

How do I multiply the pointer so 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;
}
  • What you want to multiply?

  • In case I need to show double, triple and quadruple the number typed by the user ( this should be done with pointers ) !

  • Sounds right to me. If you have some other goal, you need to ask the question.

  • Hm, so there’s no way to multiply directly when passing a value to the pointer ?

  • If you want to do something different, you need to demonstrate what it is.

  • Okay, thanks for your attention ...

Show 1 more comment

2 answers

4


The variables b , c and d are pointer types, so there will be memory addresses there. You do not want to multiply the address, just want to do the operation with 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 the auxiliaries. Much worse, right?

After you do the initialization pointing to the same variable you can create another 3 helpers to save the multiplication on them. It doesn’t seem necessary.

It seems a simple exercise and it’s correct. If it’s not, you need to have more details.

  • I wanted to multiply the value I’m passing ( &a ) by two and save on the pointer, but I understood that for this I need an auxiliary variable ... thanks for the explanation ! (:

1

&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).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.