Is there a problem in assigning a value to a pointer?

2

I'm referencing this site here How to declare pointers in C

Normally when we want to start a variable of integer type, for example, we do int inteiro = 4; m but if we do int *inteiro_ptr = 4; can it imply something in the code or for some reason not advisable? I already did some tests and did not present any problems.

#include <stdio.h>

int main(void) {

    int inteiro = 4;
    int *inteiro_ptr = 4;

    printf("Valor da variariavel 'inteiro': %d\n", inteiro);
    printf("Endereco da variariavel 'inteiro': %d\n", &inteiro);
    printf("Valor armazenado no ponteiro 'inteiro_ptr': %d\n", inteiro_ptr);
    printf("Endereco armazenado no ponteiro 'inteiro_ptr': %d\n\n", &inteiro_ptr);

    printf("Apos o uso dos ponteiros, vamos aponta-los para NULL\n\n");
    inteiro = NULL;
    inteiro_ptr = NULL;
    printf("Endereco armazenado no ponteiro 'inteiro': %d\n", inteiro);
    printf("Endereco armazenado no ponteiro 'inteiro_ptr': %d\n", inteiro_ptr);

    return 0;
}
    
asked by anonymous 22.06.2016 / 20:05

1 answer

5

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 it overrides it), so it refers to position 4 (5th byte) of the virtual memory of your application. This is certainly not what you want and is accessing an undue location.

Depending on the build options, the code does not even compile. See the ideology with safe options. Also in CodingGround .

A pointer variable should always store a memory address. This address will be used to access an object that will contain the value you want to actually store. This is a form of indirection.

In general, the referencing object is stored in the heap . Nothing forces it to be there, the example of the question even shows this, but it is the most common.

To allocate memory in heap the most common is to use the malloc() . It requests the required memory (if it is the case asks for the operating system) and returns a pointer to that portion of memory for the code. Then your code should put the value you want there in the allocated object.

Improved code (I do not know if it does what I wanted, 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;
}

See running on ideone and on CodingGround .

Note that I did not even try to access the value of inteiro_ptr after I undercut it, this would give run-time error. I had to cast cast to meet the printf() requirement. .

It has a lot of small details but I think it is already beyond the scope of the question.

    
22.06.2016 / 20:45