Output a C code with pointers

Asked

Viewed 359 times

17

I need to understand what each of the values printed on the screen means by the following code:

#include<stdio.h>

int main() {
 int i=5;
 int *p;
 p = &i;
 printf("%u %d %d %d %d \n", p, *p+2,**&p,3**p,**&p+4);
 return 0;
}

From what I understand, two variables were declared i and p, however p was declared as a pointer (I believe this is it). When he says that p = &i; means that now the value of p is the address of i. But I couldn’t understand the values of *p+2,**&p,3**p,**&p+4..

The output shows the following when executing the code:

6487628 7 5 15 9
  • 4

    It has explanation for all taste ;) So it is good at Sopt.

  • 1

    Yes, I thank each of the answers.

4 answers

18


The asterisk is the dereference operator, that is, it takes the value that is in that address. It can only be used on pointers to give correct results.

*p is to get the value of the address of p, therefore in this case the p is the address of i which you already know, then it takes the value 5 and then sum 2 giving 7.

Then he does something unnecessary, I believe only to demonstrate the functioning. He’s picking up the address of p (&p), and with it is taking the value of this address through the * (*&p) returning to have the address contained in p, so get your i who was in p, then he again takes the value of i (**&p). So there are 3 operators there: * * & p or if you prefer (*(*(&p))).

The other read like this 3 * (*p). The beginning is simple is basic arithmetic that you already know, and what comes next you have already learned above. It is taking the value that is in the address of p which we know is worth 5 (the value of i) and multiplies by 3.

The latter is a mixture of the second and third.

Separating operations to better view:

#include <stdio.h>

int main() {
    int i = 5;
    int *p = &i;
    printf("%u\n", p);  //é o endereço de i
    printf("%d\n", *p);  //é o valor de i obtido pelo endereço que está em p
    printf("%d\n", (*p) + 2); //pega o valor de i e soma 2
    printf("%d\n", (&p));  //pega o endereço de p
    printf("%d\n", (*(&p))); //com o endereço de p pega o valor dele, que é o endereço de i
    printf("%d\n", *(*(&p))); //então pega o valor de i, isto é o mesmo que *p
    printf("%d\n", 3 * (*p)); //multiplica 3 pelo valor de i, é o mesmo que 3 * i
    printf("%d\n", *(*(&p)) + 4);  //soma 4 em i através de uma fórmula desnecessária
}

Behold working in the ideone. And in the Coding Ground. Also put on the Github for future reference.

And if you’re wondering if the * has different meaning depending on the context, yes, it has, it can be used as a multiplier when we are talking about normal values or it can have the way to access the value of a pointer when we are accessing a pointer. It’s confusing, it shouldn’t be like this, but that’s how language was conceived. The 3**p demonstrates this well, the same symbol is doing two completely different operations.

15

to reply by @Maniero has a more explanatory bias of the operations, this mine has a bias of how the operations are made, without further details

  • p ==> 6487628: this is the address of i in memory
  • *p+2 ==> 7: the sum of i with 2; how p points to i, *p takes the value within i
  • **&p ==> 5: the value indicated by the value indicated by the address of p; from right to left:
    1. &p takes the address of the variable p
    2. *&p to which the address of p points, so picks p
    3. **&p, we already know that *&p ==> p of the previous step, so this equals *p, which is the numerical value 5 (p points to i, i worth 5)
      • pointer, address and variable direction, * and & are reverse operations, so they end up canceling themselves; there is only one detail, the reference operator & may be applied only once consecutively, while the right-of-way operator * can be cascaded consecutively
  • 3**p ==> 15: 3 multiplied by the value indicated by p; the first * is the multiplication operator, whereas the second is the access to the memory region
  • **&p+4 ==> 9: we already know that **&p is the value of i, then we add 4, getting 9

14

  • p appears as the value 6487628. This is the actual value stored in p, which is the address of i.

  • *p+2, due to the precedence of operators, should be interpreted as (*p)+2. *p returns the value stored in the memory address pointed by p, that is 5. Adding up, we have the result 7.

  • **&p is the same as *p because * and & cancel. One is the operation of creating a pointer from a variable and the other is a value from a pointer. We have seen that *p is 5.

  • 3**p is interpreted as 3*(*p), again, due to the precedence of the operators. That’s the same as 3*5, that is 15.

  • **&p+4 is the same as *p+4 as seen in the other case. This is interpreted as (*p)+4, that is 5+4, which is equal to 9

14

Follow your commented code:

#include <stdio.h>

int main()
{
    int i = 5;     /* Atribui 5 ao inteiro 'i' */
    int *p;        /* Declara um ponteiro 'p' para inteiro */
    p = &i;        /* Atribui ao ponteiro 'p' o endereco de 'i' */

    printf("%u\n", p );         /* Endereco armazenado no ponteiro 'p' */
    printf("%d\n", *p + 2 );    /* Soma 2 ao conteudo do inteiro apontado por 'p' */
    printf("%d\n", **&p );      /* Conteudo do inteiro apontado por 'p' (Mesmo que '*p') */
    printf("%d\n", 3 * *p );    /* Multiplica por 3 o conteudo do inteiro apontado por 'p' */
    printf("%d\n", **&p + 4 );  /* Soma 4 ao conteudo do inteiro apontado por 'p' (Mesmo que '*p + 4') */

    return 0;
}

Browser other questions tagged

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