Alexei A. Frounze wrote:
<ca***********@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com... I am working with some code that is doing some pointer to structure
casts and I can't figure out how the cast is being done. Here is
basically the code.
... typedef struct diffRecord
{
struct record *next[1];
} DiffRecord;
typedef struct record
{
struct record *next;
int value;
} Record;
int main(int argc, char* argv[])
{
Record *rec1 = malloc(sizeof(Record));
rec1->value = 5;
Record *rec2 = malloc(sizeof(Record));
rec2->value = 6;
rec1->next = rec2;
rec2->next = NULL;
DiffRecord *diffRec = ((DiffRecord*)rec1)->next[0];
Record *testRec = (Record*)diffRec;
printf("The value is: %i\n", testRec->value); //prints 6
... How is the pointer contained in record being coerced into the array in
the diffRecord structure?
Here:
struct record *next[1];
struct record *next;
One struct contains array of 1 pointer.
The other contains 1 pointer, w/o being part of any array.
But, they both contain a pointer anyway, so there's no problem here. It's
not much different from the case when you have an array of 1 int and just 1
int, simply the type is different.
So, both structs contain in their beginning a pointer (or array of one
pointer -- doesn't matter how you think of it, in this particular case it's
essentially the same thing and from the memory's standpoint the two things
are equivalent)...
I can only say that this is a bit odd code... I'd expect this way of casting
Record *testRec = (Record*)diffRec;
rather than this
DiffRecord *diffRec = ((DiffRecord*)rec1)->next[0];
But you have both. What for?
Alex
This code actually is from another author. Their code looks something
like this:
#include <stdio.h>
#include <stdlib.h>
typedef struct diffRecord
{
struct diffRecord *next[2];
} DiffRecord;
typedef struct record
{
struct record *next;
struct record *previous;
int value;
} Record;
static void printValue(int index, Record *rec)
{
DiffRecord *diffRec = (DiffRecord*)rec;
Record *testRec = (Record*)diffRec->next[index];
printf("Value: %i\n", testRec->value);
}
int main(int argc, char* argv[])
{
Record *rec1 = malloc(sizeof(Record));
Record *rec2 = malloc(sizeof(Record));
Record *rec3 = malloc(sizeof(Record));
rec1->value = 5;
rec2->value = 6;
rec3->value = 7;
rec1->next = rec2;
rec1->previous = NULL;
rec2->previous = rec1;
rec2->next = rec3;
rec3->previous = rec2;
rec3->next = NULL;
printValue(0, rec2);
printValue(1, rec2);
free(rec3);
free(rec2);
free(rec1);
return 0;
}
Output:
Value: 7
Value: 5
I think their intention was for the parameter index in printValue() to
be some sort of selector for the pointers in the rec parameter. The
haven't seen a cast like this before but I am assuming that this works
becuase the first two parameters of Record are pointers?
If say Record looks like this:
typedef struct record
{
struct record *next;
int value;
struct record *previous;
} Record;
and I cast struct record to a struct diffRecord would I get some
undefined behaviour trying to access next[1] field of diffRecord?
Thanks for the help,
Caleb Van Dyke