Problem with pointer and array

1

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

asked by anonymous 06.10.2017 / 05:05

2 answers

4
  

which corresponds to the 8 bits of an integer

A int does not have 8 bits, has at least 16 bits, but most implementations and architectures are 32 bits.

When you try to move 8 in an array it is making 8 bytes and not bits.

Pointer arithmetic is always based on the size of the type. If you pay attention the second pointer is 32 bytes, ie 8 times the 4 bytes of each integer, which obviously accesses an undue loop since the array has 32 bytes, so the first effective offset does fall out of it. It should not multiply by 8.

  

Because in the code below the location of the vector variable is not summed with 8 and assigned to the pointer?

Nothing is added by 8 there. Simple math.

  

[1] use the starting location

No, [0] is the starting location.

As the initial premise is wrong or try to understand matrix in this way.

Read more in Arrays are pointers? .

    
06.10.2017 / 05:22
3

Trying to complement the already very good and complete answer of @Maniero, I present your code to show the memory locations and values for each element of the array:

#include <stdio.h>
#include <stdlib.h>

void main()
{

    int vetor [4] = {1,2,3,4 };
    int i;
    int *pt = vetor; //ponteiro começa a apontar para o primeiro elemento do vetor

    for(i = 0; i < 4; i++)
    {



        printf("\nLocal: %p, Valor: %d\n", pt, *pt);
        //----------------^ Para mostrar o endereço de memoria tem de ser %p
        pt++; //so pode aumentar o ponteiro no fim, depois de utilizar
    }
}

Output:

Local: 0x7ffebfbb5eb0, Valor: 1

Local: 0x7ffebfbb5eb4, Valor: 2

Local: 0x7ffebfbb5eb8, Valor: 3

Local: 0x7ffebfbb5ebc, Valor: 4

See this example in Ideone

Note that to walk through the various elements of the array through the pointer, it was only necessary to increase with pt++ . Since the compiler knows that it is a pointer to int then it advances the necessary bytes at a time, corresponding to sizeof of int , which will normally be 4 but depends on implementation and architecture as @hand said.

    
06.10.2017 / 12:28