Char vector cast for pointer

Asked

Viewed 1,602 times

5

People how a cast works in a vector char for a pointer int?
for example:

char vetor[2];
int *p;
p = (int *)&vetor;

Can someone explain me this line?

  • This code will take each element of the char vector, convert to integer and place in the integer vector..

  • As far as I understand, this creates a pointer to an integer value which in turn is the char vector memory address (of the first element of the vector). Out of context like this, no use at all.

1 answer

4

First it is necessary to understand what exactly a pointer stores, regardless of the type of pointer it will have as its main function to store an address, which can be any memory location as variables, functions of your program and even external functions such as Dlls and others. On 32-bit machines the pointers usually have 4 bytes, which can point anywhere from the first almost 4GB of memory and on 64-bit machines the pointers usually have 8 bytes, which is enough to map anywhere in the memory of much larger quantities.

When you have a type with a pointer (for example: void* out int*), you are just informing the compiler, how many bytes from the address are representing the value. See this image:

inserir a descrição da imagem aqui

As you can see in the image above, the P2 pointer only points to address 5 and the fact that its type is int (4 bytes), when you will get the value where the P2 pointer is pointing it will get the bytes of the address 5 + those of the size reported to the pointer (4 in this case), ie the value of the int shall be the values of the positions in memory 5, 6, 7 and 8.

When you have a pointer void, this pointer continues with its function of storing addresses, however it is not possible to obtain value, because the type void does not represent any size, so the only utility of having a pointer void is the storage of the address in memory.

But remember again, regardless of whether the pointer type is void or int it continues to occupy the 4 bytes (or 8 in 64 bits) to store the address at which it is pointing.

In the case of your code, when you point int *p; for char vetor[2]; the pointer int *p; will only store the address of char vetor[2];, this cast p = (int *)&vetor; is only to pass the vector address as if it were the same type, however it will not be possible to get the value of that address without doing another cast, because when trying to get the value of *p as if he were a int, it would eventually get the value of two more bytes that does not belong to the char vetor[2];, see why:

inserir a descrição da imagem aqui

As you can see in the image above char vetor[2]; occupies 2 bytes (as each char takes 1 byte), when you pass the address of char vetor[2]; for int *p; with a cast and try to get the value of int *p; it would consider that the value would be the address at which *p points the size of the type to it reported (in this case int = 4 bytes), that is, it would get the two bytes of array vector, and still get two bytes more out of the array vector to form a value int.

If you want to pass characters, numbers or strings in pointer form int, this is possible, but then you have to do another cast to represent the value again, see this example.

// Transforma em ponteiro int
char vetor[] = "Hello World!";
int *p = (int *)&vetor;
// Trannsforma em ponteiro char
char *text = (char*)p;
printf("%s", text);
// Output: Hello World!

However if the focus is only to pass an address, it is better to use pointer void that will not get more bytes in memory than the given size. See this example of pointer void that points to function:

#include <stdio.h>

int GetFive()
{
    return 5;
}

typedef int (*FUNÇÃO)();

void MostraRetornoDaFunção(void* função) // Esta função recebe uma função como um ponteiro void
{
    int (*fun)() = (FUNÇÃO)função; // Aqui é necessario fazer um cast para mostrar que o *void é uma função
    int a = fun();
    printf("%d", a);
}

int main()
{
    void *funcPtr = &GetFive;
    MostraRetornoDaFunção(funcPtr);

    printf("\n");
    system("PAUSE");
}

So basically the cast served only to pass the address to the pointer as if the variable pointed was of the same type, and the type served only to know which bytes after the given address will be used as value.

I hope I’ve helped

  • 11/10 Perfect explanation

Browser other questions tagged

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