This is completely wrong. What this code is doing is to say that the variable inteiro_ptr
has the value 4 (then the value 0 when cancels it), therefore it refers to position 4 (5o. byte) of the virtual memory of your application. This is certainly not what you want and is accessing an improper location.
Depending on the build options, the code does not even compile. See in the ideone. And in the repl it.. Also put on the Github for future reference.
A pointer type variable must store a memory address at all times. This address will be used to access an object that contains the value you want to store for truth. This is a form of indirect.
In general the referencing object is stored in the heap. Nothing obliges it to be there, the example of the question even shows it, but it is the most common.
To allocate memory in heap the most common is to use the function malloc()
. It requests the required memory (if it asks for the operating system) and returns to the code a pointer to that portion of memory. Then your code should put the value you want there in the allocated object.
Improved code (I don’t know if you do what you want, but it’s more correct):
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int inteiro = 4;
int *inteiro_ptr = malloc(sizeof(int));
*inteiro_ptr = 4; //aqui está colocando o valor no endereço apontado pela variável
printf("Valor da variariavel 'inteiro': %d\n", inteiro);
printf("Endereco da variariavel 'inteiro': %p\n", (void *)&inteiro);
printf("Valor armazenado no ponteiro 'inteiro_ptr': %d\n", *inteiro_ptr);
printf("Endereco armazenado no ponteiro 'inteiro_ptr': %p\n\n", (void *)inteiro_ptr);
printf("Apos o uso dos ponteiros, vamos aponta-los para NULL\n\n");
inteiro = 0; //esta variável não é um ponteiro, quer zerá-la, faça com 0
inteiro_ptr = NULL;
printf("Endereco armazenado no ponteiro 'inteiro': %d\n", inteiro);
printf("Endereco armazenado no ponteiro 'inteiro_ptr': %p\n", (void *)inteiro_ptr);
return 0;
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Note that I didn’t even try to access the value of inteiro_ptr
after cancelling it, this would give error in execution time. I had to make a cast to meet the requirement of printf()
.
It has a lot of little details but I think it’s already beyond the scope of the question.
The bigown has already said it all. I just wanted to complement by saying that ,for example,
int *var_ptr;
you are only declaring a pointer to an integer which often confuses us is * because usually when we put * it is to point the pointer to a certain value. When doesint *var_ptr = &var;
is already correct because it is the same thing thatint *var_ptr;
and thenvar_ptr = &var;
. My advice is when you want to declare a pointer to a certain variable of a certain type doint* var_ptr;
left * glued to int instead of glued to variable name I think confuses less.– Tiago Martins
I agree with @Mttech, as long as you never declare more than one variable on the same line, if you do this, it gives @#$%&!*.
– Maniero
Did the answer solve what you were looking to know? Do you think you can accept it now? If not, you need something else to be improved?
– Maniero