C
caleb.vandyke
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.
#include <stdio.h>
#include <stdlib.h>
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
free(rec1);
free(rec2);
return 0;
}
How is the pointer contained in record being coerced into the array in
the diffRecord structure?
Caleb Van Dyke
casts and I can't figure out how the cast is being done. Here is
basically the code.
#include <stdio.h>
#include <stdlib.h>
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
free(rec1);
free(rec2);
return 0;
}
How is the pointer contained in record being coerced into the array in
the diffRecord structure?
Caleb Van Dyke