Is there a problem assigning a value to a pointer?

Asked

Viewed 271 times

3

I’m referencing myself by this site here How to declare pointers in C

Usually when we want to start an integer type variable, for example, we do int inteiro = 4;but what if we did int *inteiro_ptr = 4; may imply something in the code or for some reason is not advisable? I have done some tests and presented no problem.

#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;
}
  • 1

    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 that int *var_ptr; and then var_ptr = &var;. My advice is when you want to declare a pointer to a certain variable of a certain type do int* var_ptr; left * glued to int instead of glued to variable name I think confuses less.

  • I agree with @Mttech, as long as you never declare more than one variable on the same line, if you do this, it gives @#$%&!*.

  • 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?

1 answer

9

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.

Browser other questions tagged

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