I wanted to print the corresponding places and values from an array using a pointer, but when I ask the pointer to add the location of the vector with 8 (which corresponds to the 8 bits of an integer) program sums up values that I do not even know where they left off.
Because in the code below the location of the vector variable 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 outputs:
Local: -1259032880, valor: 1
Local: -1259032848, valor: 4195888
Local: -1259032816, valor: -1259032600
Local: -1259032784, valor: 4195520
Output 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 two-dimensional arrays, that is, arrays. Is the way to sum an array the same as for a simple one-dimensional array ? For example:
int vetor [4];
[1] uses the starting location and assigns the pointer [2] adds the initial location to 8 and assigns the pointer [3] adds the initial location with 2 * 8 and assigns the pointer [4] adds the initial location to 3 * 8 and assigns it to the pointer.
int matriz [2] [2];
[1] [1] uses the initial location and assigns the pointer [1] [2] adds the initial location to 8 and assigns the pointer [2] [1] sum 8 twice? [2] [2] sum three times?
That is, the array location in memory is that when the allocation of [1] [2] is finished, then the value of [2] [1] 8 bits after the previous one is allocated? >