failure to assign values to variables with pointers

1

Problem with pointers

I assign value to the first variable using a pointer, then reuse the same pointer and assign a value to the next variable but I do not understand why the last one prints a value that has nothing to do with the code (== 37 )
I wanted to know why this happens.

    
asked by thomasnx 29.09.2018 в 03:52
source

1 answer

1

Your error is here:

*puntero=&var1;

Actually, by declaring the variable this way:

int *puntero=&var;

It is to declare pointer as just, a pointer to a direction that in this case will be that of var, the error you make when doing:

*puntero=&var1;

Since pointer is already a pointer, you do not need to put * again.

Directly, you put:

puntero=&var1;

But it is like saying that the pointer to the pointer content is equal to the address of var1, but since that is not causing the pointer to point to var1, the error occurs when trying to change the value later with:

 *puntero=4;

The number that appears in the printf is the result of a variable without a value, it is like when you exceed a vector of characters, the value n + 1 will have an odd symbol and the next and the next one, since you are consulting places of memory completely indifferent to your code (?

    
answered by 29.09.2018 / 04:52
source