c_cpp 比较c中任何类型的指针

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 比较c中任何类型的指针相关的知识,希望对你有一定的参考价值。

#include "stdio.h"
#include "stdlib.h"


#define XenObject_HANDLE unsigned char type; \
        struct XenObject * next; \
        struct XenObject * prev;

struct XenObject
{
    XenObject_HANDLE
};

typedef struct XenObject XenObject;






#define XenObject_GETTYPE(xo) xo->type

#define XenObject_SETTYPE(xo, t) xo->type = t

#define XenObject_SAMETYPE(xo1, xo2) xo1->type == xo2->type

#define XenObject_CASTOBJ(xo) (XenObject*)xo

#define XenObject_CONNECT_NEXT(xo1, xo2) do { \
                    xo1->next = xo2; \
                    xo2->prev = xo1; } while(0)
                    
#define XenObject_CONNECT_PREV(xo1, xo2) do { \
                    xo1->prev = xo2; \
                    xo2->next = xo1; } while(0)
                    
#define XenObject_IS_END(xo) xo->next == NULL || xo->prev == NULL

//advances pointer to back of stream
#define XenObject_ADV_BACK(xo) while(xo->next != NULL) xo = xo->next

//advances pointer to front of stream
#define XenObject_ADV_FRONT(xo) while(xo->prev != NULL) xo = xo->prev

#define XenObject_APPEND(xo1, xo2) do { \
     while(xo1->next != NULL) xo1 = xo1->next; \
     xo1->next = xo2; \
     xo2->prev = xo1; \
    }while(0)
    
#define XenObject_PUT(xo1, xo2) do { \
     while(xo1->prev != NULL) xo1 = xo1->prev; \
     xo1->prev = xo2; \
     xo2->next = xo1; \
    }while(0)
     
     

int main(void) {
  XenObject* one = malloc(sizeof(XenObject));
  XenObject* two = malloc(sizeof(XenObject));
  XenObject_SETTYPE(two, 3);
  XenObject_CONNECT_NEXT(one, two);
  XenObject* three = malloc(sizeof(XenObject));
  XenObject_SETTYPE(three, 7);
  XenObject_APPEND(one, three);
  printf("two's type is %d\n", XenObject_GETTYPE(one->next));
  one = one->next;
  printf("is same %d\n", three == one);
  //1
  return 0;
}

以上是关于c_cpp 比较c中任何类型的指针的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 给出链表,使得每个节点包含一个附加的随机指针,该指针可以指向列表中的任何节点或为空。返回

c_cpp C ++的序列容器类,其作用类似于可以连接的字符串,但适用于任何类型。

c_cpp 在c中移动一个字节指针

c_cpp 在虚空中持有指针的实验

c_cpp 在c中使用void指针数组的一些练习

c_cpp 从c中的未被发现的指针中获取值