1
I wanted to print the corresponding locations and values of a array using a pointer but when I ask the pointer to add the vector location to 8 (which corresponds to the 8 bits of an integer) the program adds values that I don’t even know where it came from.
Because in the code below the vector variable location is not summed with 8 and assigned to the pointer?
#include <stdio.h>
#include <stdlib.h>
void main()
{
int vetor [4] = {1 ,2 ,3 ,4 };
int i;
int *pt;
for(i = 0; i < 4; i++){
pt = vetor + i * 8;
printf("\nLocal: %d, Valor: %d\n", pt, *pt);
}
}
Follow one of the exits:
Local: -1259032880, valor: 1
Local: -1259032848, valor: 4195888
Local: -1259032816, valor: -1259032600
Local: -1259032784, valor: 4195520
Exit that theoretically should happen:
Local: 1259032840, valor: 1
Local: 1259032848, valor: 2
Local: 1259032856, valor: 3
Local: 1259032864, valor: 4
And another question is about arrays two-dimensional, i.e., matrices. The way to sum a matrix is the same as that of a array simple one-dimensional? For example:
int vetor [4];
[1] uses the initial location and assigns to the pointer [2] sum the initial location with 8 and assign to the pointer [3] sum the initial location to 2 * 8 and assign to pointer [4] sum the initial location with 3 * 8 and assign to the pointer.
int matriz [2] [2];
[1] [1] uses the initial location and assigns to the pointer [1] [2] sums the initial location with 8 and assigns to the pointer [2] [1] sum 8 twice? [2] [2] sum three times?
That is, the local hierarchy of the matrix in memory is that when the [1] [2] allocation ends, then the [2] [1] 8-bit value is allocated after the previous?