//online parser:
https://www.bccn.net/run/
程序代码:
#include <stdio.h>
#include <stdlib.h>
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
typedef struct _ElemSH
{
int data;
struct _ElemSH *next;
}ElemSH, *PSH;
int main(int argc, char *argv[]);
void* init_ElemSH(void *h, void *m)
{
int i;
//BUG_ON(!h);
#define CAST2ELEM(v, idx) ((PSH)(v))[idx]
for(i = 0; i < 10; i++) {
typedef unsigned char uint8_t;
CAST2ELEM(h, i).data = ((uint8_t *)m)[i] % 10;
CAST2ELEM(h, i).next = (PSH)(&CAST2ELEM(h, i + 1));
}
return h;
}
void print_ElemSH(void *h)
{
int i;
//BUG_ON(!h);
for(i = 0; i < 20; i++) {
printf("h[%02d]->data = %d\t", i, (&CAST2ELEM(h, i))->data);
#define MASK_A(_a) _a & 0xfff
printf("h[%02d]->next = 0x%x\n", i, MASK_A((int)(&CAST2ELEM(h, i))->next));
}
}
void* merge_ElemSH(void *va, void *vb)
{
int i;
//BUG_ON(!h);
for(i = 0; i < 10; i++) {
if(CAST2ELEM(va, i).data <= CAST2ELEM(vb, i).data) {
CAST2ELEM(va, i).next = &CAST2ELEM(vb, i);
CAST2ELEM(vb, i).next = &CAST2ELEM(va, i + 1);
} else {
//do sth...
}
}
return va;
}
int main(int argc, char *argv[])
{
struct _ElemSH ha[10];
struct _ElemSH hb[10];
init_ElemSH(ha, main);
#define safe_region +0x100
init_ElemSH(hb, main + safe_region);
//#define PRINT_ON
#ifdef PRINT_ON
puts("----------ha & hb list--------");
print_ElemSH(ha);
puts("----------merge list--------");
print_ElemSH(merge_ElemSH(ha, hb));
#endif
return 0;
}
index & pointer just 4 fun