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

Asked

Viewed 77 times

2

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

Write a code in C that uses a variable with the name 'root', have the type 'float' and have the initial value 3.1415.

In this same code, use another variable with the name ' pointing to root' that allows you to store the memory address of the above-mentioned root ? .

Finally, modify the contents of the variable root átiroot' by the variable '_para_root' so that the value of root' is 3.141592. At the end of the code, use printf to print on the screen the value of the 'root' variable.

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;
}   

2 answers

5


The fact of being a root is orthogonal, that is, it does not matter for the question.

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

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

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

Print aponta_para_raiz for curiosity will see the memory address where the variable is raiz. For this you have to use the %p in the printf(). At 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
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • printf("MODIFIED ROOT = %f", *pointing to root); //I don’t think it’s necessary (Really, it’s not necessary!)

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 pointing to root point to root :) so first we must create

float *aponta_para_raiz;

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

now we want her to point to root for this we do

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

that means that pointing to root receives the root reference, ie your address.

and in doing

*aponta_para_raiz = 3.141592;

we tell the compiler who is pointed by pointing to root receives 3.141592, ie, root receives 3.141592.

Browser other questions tagged

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