How to modify the contents of a root using a pointer?

2

How to modify the contents of a root using a pointer?

  

Type a code in C that uses a variable named 'root', has the type 'float' and has the initial value number 3.1415.

     

In this same code, use another variable named 'point_to_race' to store the memory address of the above-mentioned 'root' variable.

     

Lastly, modify the contents of the 'root' variable by using the 'point_to_race' variable so that the 'root' value is 3.141592.   At the end of the code, use printf to print the value of the 'root' variable on the screen.

From 3.1415 to 3.141592

 #include <stdio.h>

 int main()
{

  float raiz = 3.1415;
  int *aponta_para_raiz;

  printf(" RAIZ = %f\n", raiz );
  printf (" RAIZ MODIFICADA = %d", *aponta_para_raiz);


return 0;
}   
    
asked by anonymous 20.08.2018 / 20:58

2 answers

5

Being a root is orthogonal, that is, it does not matter to the question.

Of course, doing this is unnecessary, but I understand that you want to do it just to exercise.

Whenever you access the value of a variable being pointed to by another variable you have to use the dereference operator. This operator is the opposite of the & operator which is the "address of". So in your code you have to store aponta_para_raiz in the raiz address, therefore &raiz . And then when you access the root value you will have to *aponta_para_raiz . If you do not use this operator you are accessing the address itself and not the value that is in the address.

Notice that you have two errors linked in the code. The pointer created is a int * that reads like "Pointer to int ", but what you want is a pointer to float . And then use %d in printf() when right is %f .

If you print aponta_para_raiz out of curiosity you will see the memory address where the raiz variable is. For this you have to use %p in printf() . In the execution I did this, click and see there.

#include <stdio.h>

int main() {
    float raiz = 3.1415;
    float *aponta_para_raiz = &raiz;
    *aponta_para_raiz = 3.141592;
    printf("RAIZ = %f\n", raiz);
    printf("RAIZ MODIFICADA = %f", *aponta_para_raiz); //acredito não ser necessário
}

See running on ideone . And in Coding Ground . Also put it in GitHub for future reference .

    
20.08.2018 / 21:07
2
#include <stdio.h>

int main()
{

  float raiz = 3.1415;
  float *aponta_para_raiz;

  aponta_para_raiz = &raiz;

  *aponta_para_raiz = 3.141592;

  printf(" RAIZ = %f\n", raiz );
  printf (" RAIZ MODIFICADA = %f", *aponta_para_raiz);


    return 0;
}

Our goal is to make point_to_raise point to root :) so we must first create

float *aponta_para_raiz;

this variable holds an address of a float, which in our case will be root it must be of the same type of the value pointed out, that is, we must create a float pointer to point to a float.

Now we want it to point to root for this we do

// aponta_para_raiz recebe a referência de raiz
aponta_para_raiz = &raiz;

This means that point_to_race gets the root reference, ie your address.

and when doing

*aponta_para_raiz = 3.141592;

We tell the compiler that whoever is pointed to by point_to_root gets 3.141592, root receives 3.141592.

    
20.08.2018 / 21:09