ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]警告问题及双向链表的地址问题

ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]警告问题及双向链表的地址问题,第1张

ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]警告问题及双向链表的地址问题

1.报此警告的意思是,在同一块中的非声明之后不能有声明。请把所有的声明变量放在函数的所有的初始化之前。

2.编码误区,C中64位系统的指针大小就是8字节,申请的结构体指针必须按照如下格式进行置空 *** 作。

typedef struct information{
    int a;
    char b;
    ...
} Info, *INFO;


struct information *in = (struct information*)malloc(sizeof(struct information));
错误做法:memset(in, 0x00, sizeof(in)); !!!!! in是个指针,8字节,初始化并不彻底,内核模块编译报警告。
正确做法:memset(in, 0x00, sizeof(Info));

3.链表中的结构体指针问题

#include 
#include 
#include 
#include 
#include 

typedef struct information{
    int a;
    int b;
    struct information *prev;
    struct information *next;
} Info, *INFO;

//注意与内核的宏container_of(用于通过给出的一个结构体定义和已经实例化的结构体其中一个变量,顺藤摸瓜找出此实例化的结构体首地址)的使用情景相区分
//内存释放就省略了,注意free节点后头指针并不指向NULL



void showlink(INFO head){
    assert(NULL != head);
    INFO temp = head->next; //定义一个指针指向头节点的下一个节点的首地址,注意,这里并不是下一个节点的prev!!!
    printf("下一节点的首地址n");
    printf("head->next=[%p]n", head->next);//打印指针中存放的地址
    printf("temp------=[%p]n", temp      );//打印下一个节点的首地址
    printf("当前节点的首地址n");
    printf("temp->prev=[%p]n", temp->prev);//打印下一个节点的prev地址
    printf("head------=[%p]n", head      );//打印当前节点的首地址

}


int main(){
    INFO head = NULL;
    int  i;
    //create a link
    for(i=0; i<10; i++){
        INFO new_node = (INFO)malloc(sizeof(Info));
        memset(new_node, 0x00, sizeof(Info));
        
        if(NULL == head){
            head = new_node; //head存放的是node节点的首地址,而不是node节点的prev的地址
            new_node->prev = NULL;
            new_node->next = NULL;
        }else{//头插法
            new_node->prev = NULL;
            new_node->next = head;
            head->prev = new_node;
            head = new_node;
        }
    }

    showlink(head);
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5635179.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存