Cast of char vector for pointer

5

How does a cast work in a char vector for a int pointer? for example:

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

Can anyone explain this line to me?

    
asked by anonymous 24.06.2015 / 03:25

1 answer

4

First you need 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 like variables, functions of your program and even external functions like DLLs and others. In 32-bit machines pointers usually have 4 bytes, which can point to anywhere in the first almost 4GB of memory, and in 64-bit machines pointers are usually 8 bytes long, which is enough to map any memory space of much larger amounts. / p>

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

Asyoucanseeintheimageabove,thepointerp2onlypointstoaddress5andbecauseitstypeisint(4bytes),whenyougetthevalueatwhichthepointerp2ispointingitgoesgetthebytesoftheaddress5+thoseofthesizeinformedtothepointer(4inthiscase),ie,thevalueofintwillbethevaluesofthememorypositions5,6,7and8.

Whenyouhaveavoidpointer,thispointercontinueswithitsaddressstorefunction,butitisnotpossibletogetvaluebecausevoiddoesnotrepresentanysize,sotheonlyutilitytohaveavoidisthestorageoftheaddressinmemory.

Butrememberagain,regardlessofwhetherthepointertypeisvoidorintitcontinuestooccupythe4bytes(or8in64bits)tostoretheaddressitispointingat.

Inthecaseofyourcode,whenyoupointint*p;tocharvetor[2];theint*p;pointerwillonlystorethecharvetor[2];address,thisp=(int*)&vetor;castisonlytopassthevectoraddressasifitwerethesametype,butyouwillnotbeabletogetthevalueofthataddresswithoutdoinganothercast,becausewhentryingtogetthevalueof*pasifitwereaint,itwouldendupgettingthevalueoftwomorebytesthatdoesnotbelongtocharvetor[2];,seewhy:

As you can see in the image above, char vetor[2]; occupies 2 bytes (since each char occupies 1 byte), when you pass the char vetor[2]; address to int *p; with a cast and try to get the value of (for example, int = 4 bytes), that is, it would get the two bytes of the array vector, and would still get two more bytes out of% w /% vector to form a int *p; value.

If you want to pass characters, numbers or strings in the form of pointer array , 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!

But if the focus is just to pass an address, it is best if you use int pointer that will not get more bytes in memory than the given size. See this example of pointer int 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 would only serve to pass the address to the pointer as if the variable pointed to were of the same type, and the type would only serve to know which bytes after the given address will be used as value.

I hope I have helped

    
27.06.2015 / 05:04