The next two lines mean the same thing?
*((int*)(direccion + OFF_TABLE_KEY+sizeof(int))) = sec2;
((int*)(direccion + OFF_TABLE_KEY))[1] = sec2
The syntax puntero[entero]
equals *(puntero + entero)
, and the pointer arithmetic causes that entero
to be computed as entero*sizeof(tipo)
, where tipo
is pointed by the pointer.
In the previous case, puntero
would be the expression ((int*)(direccion+OFF_TABLE_KEY))
. Even without knowing the type pointed to by direccion
, the result of direccion+OFF_TABLE_KEY
will return another pointer (to an unknown type), but since the casting then forces the interpretation of that pointer as int*
, then putting a [1]
after is equivalent to adding the size of an integer to the result.
Therefore, yes , are equivalent, provided that direccion
is a pointer to a type that occupies 1 byte (ex: char*
). Otherwise the address to which the first expression would access would be different, since the compiler would still be multiplying sizeof(int)
by the size of the type pointed to by direccion
, because the complete expression (direccion + OFF_TABLE_KEY+sizeof(int))
is evaluated using pointer arithmetic .
In the absence of information on the type of direccion
, it can not be guaranteed that they are equivalent, but I can assure you that the following two expressions would be equivalent (by forcing the type of direccion
to char*
):
*((int*)((char*)direccion + OFF_TABLE_KEY+sizeof(int))) = sec2;
((int*)((char*)direccion + OFF_TABLE_KEY))[1] = sec2
Can someone tell me what the following lines do?
num = *((int*)(direccion+sizeof(char)));
(*(ST_DATA*)(direccion+posTabla*sizeof(ST_DATA))).speed = num;
The first one has already explained it to you perfectly @eferion . As for the second, again I will make the hypothesis that direccion
is a pointer to char
, otherwise the expression would not make much sense.
The expression is understood if we assume that direccion
has been declared as pointer to char
, but at this moment we have it pointing to a memory area where there is an array of structures, each of type ST_DATA
. What that expression wants is to access one of those structures (the one that is in%% of%) to save something in its posTabla
field.
If speed
were a pointer to structures, the thing would be as simple as:
direccion[posTabla].speed = num;
but the fact that direccion
is a pointer to direccion
forces us to make certain juggling with pointers and castings. If we "force" the compiler to see char
as a pointer to direccion
, then we can use a syntax very similar to the previous one:
((ST_DATA*)direccion)[posTabla].speed = num;
And the line that you have copied is just another (a bit more twisted) way of doing the same thing, doing in the explicit expression the arithmetic of pointers that in the expression that I have set the compiler would implicitly solve .
One more way to say the same thing would be:
(ST_DATA*)(direccion+posTabla*sizeof(ST_DATA))->speed = num;
in this case if dereferencing the pointer obtained by the expression, but using that pointer to access a member of the structure. ( ST_DATA
is equivalent to p->a
)