Cast from pointer to integer of different size warning
I am getting
warning: cast from pointer to integer of different size
at line return (Node *) ((unsigned int)(a) ^ (unsigned int)(b));
. How can I get rid of it ? The intent of XORing the addresses is to implement a XORed link list which is a doubly link list and it consists of only one field to traverse back and forth in the list. The npx field contained the difference between the pointer to the next node and the pointer to the previous node. The pointer difference is calculated by XORing.
typedef struct _node {
int data;
struct _node *npx;
}Node;
Node * XOR(Node *a, Node *b){
return (Node *) ((unsigned int)(a) ^ (unsigned int)(b));
}
void addNode(int num, Node **head, int position, int flag)
{
Node * temp, *q;
Node *node = (Node *)malloc(sizeof(Node));
node ->data = num;
node->npx = XOR(*head, NULL);
Node *next = XOR((*head)->npx, NULL);
(*head)->npx = XOR(node, next);
*head = node;
}
int main()
{
Node *head = (Node *)malloc(sizeof(Node));
head->data =23;
head->npx= NULL;
addNode(32, &head, 1, 0);
addNode(33, &head, 1, 0);
addNode(34, &head, 1, 0);
addNode(35, &head, 1, 0);
addNode(36, &head, 1, 0);
addNode(37, &head, 1, 0);
addNode(178, &head, 3, 1);
displayNode(&head);
return 0;
}
您可以使用#include <inttypes.h>
和一个整数类型uintptr_t
,它定义为可以从N1570 7.18.1.4中的任何有效指针转换为void
以避免警告。
Node * XOR(Node *a, Node *b){
return (Node *) ((uintptr_t)(a) ^ (uintptr_t)(b));
}
链接地址: http://www.djcxy.com/p/28386.html
上一篇: 从不同大小的整数转换为指针
下一篇: 从指针转换为不同大小的整数警告