Problem with pointer and array

Asked

Viewed 280 times

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?

2 answers

3


which corresponds to the 8 bits of an integer

A int is not 8 bits, has at least 16 bits, but in most implementations and architectures is 32 bits.

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

Pointer arithmetic is always based on type size. If you pay attention the second pointer is offset 32 bytes, that is to say 8 times the 4 bytes of each integer, which obviously accesses an improper location since the array has 32 bytes, so the first effective offset drops out of it. It should not multiply by 8.

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

Nothing is added up by 8 there. Simple math.

[1] uses initial location

No, the [0] is the initial location.

As the entire initial premise is wrong nor is it worth trying to understand matrix in this way.

Read more on Arrays are pointers?.

2

Trying to complement the already very good and complete @Maniero response, I present your code showing 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
    }
}

Exit:

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 the various elements of the array through the pointer it was only necessary to increase with pt++. How does the compiler know it is a pointer to int then it advances the required bytes at a time, corresponding to the sizeof of int, which will normally be 4 but it depends on implementation and architecture as @Maneiro already said.

Browser other questions tagged

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