• 总结


    1、

    #include<stdio.h>
    #include<stdlib.h>
    struct student
    {
        char gender;
        int id;
        int age;
    };
    int main()
    {
        struct student s;
        printf("%d
    ",sizeof(s));
        return 0;
    }

    程序的输出结果是12。因为虽然student结构体中gender只占了一个字节,但是由于在32位linux环境中,一条指令的cpu总线宽度为32位,即四个字节。对代码的处理需要保持4字节对齐,所以即便gender只有一个字节,依然占四个字节

    2、结构体指针在使用时需要进行申明

    #include<stdio.h>
    #include<malloc.h>
    struct page{
        int a,b;
    };
    struct vcpu{
        struct page *p;
    };
    int main()
    {
        struct vcpu *v;
        v = (struct vcpu *)malloc(sizeof(struct vcpu));
        if(!v)
        {
            printf("error
    ");
        }
        v->p = (struct page *)malloc(sizeof(struct page));
        if(!v->p)
        {
            printf("error
    ");
        }
        if(v->p)
        {
            printf("111
    ");
        }
        else
        {
            printf("222
    ");
        }
        return 0;
    }
    3、函数名称就是指向函数的第一条指令常量指针。即可以定义函数指针来指向函数。
    函数指针定义:type (*func)(type &,type &);
    例如:int func(int x) 声明一个函数
    int (*f)(int x) 声明一个函数指针
    f = func  将func函数的首地址赋值给指针f
     
    4、关于内核宏container_of:通过一个结构体成员变量的指针来获取到整个结构体
    内核中双线链表遍历函数 list_entry(ptr,type,member)定义为
    #define list_entry(ptr,type,member) containber_of(ptr,type,member)
    通过type结构体中的member成员变量的指针ptr,来获得指向type结构体的指针, 从而进行链表遍历。
    如:
    struct test
    {
        struct list_head list;
        int data;
    }
    struct list_head
    {
        struct list_head *prev;
        struct list_head *next;
    }
    在程序中定义了一个list_head指针:struct list_head *p = (struct list_head)malloc(sizeof(struct list_head))
    可以通过list_entry(p,struct test,list),p与list类型一样,可以得到test结构体的指针
     
    5、对于新加入的模块而言,struct module结构体是加入到内核模块链表的表头,作为了一个头结点
     
    6、C语言调用C++头文件里面声明的函数,函数应该怎么声明(C++头文件声明一个函数,C++源文件实现该函数,另一C源文件包含了该C++头文件并调用这个函数,问该函数声明的时候应该怎么做)
    在C++头文件中,声明 extern "C"
    C++头文件,1.h
    #include <iostream>
    using namespace std;
    extern "C" int fun(int a,int b);
    
    C++实现文件
    #include<iostream>
    #include "1.h"
    using namespace std;
    int fun(int a,int b)
    {
        int c;
        c=a+b;
        cout<<c<<endl;
        return 0;
    }
    
    C实现文件
    #include <stdio.h>
    extern int fun(int a,int b);
    int main(void)
    {
        fun(1,2);
        return 0;
    }

    程序输出3,所有文件应该包含在一个文件夹中

  • 相关阅读:
    使用线程新建WPF窗体(公用进度条窗体)
    WPF--模板选择
    WPF命令(Command)介绍、命令和数据绑定集成应用
    WPF 员工卡条形码
    WPF入门教程系列(二) 深入剖析WPF Binding的使用方法
    R语言——实验5-聚类分析
    R语言——实验5-聚类分析
    Java学习---流与文件
    Java学习---流与文件
    Java学习---异常处理
  • 原文地址:https://www.cnblogs.com/scu-cjx/p/6878512.html
Copyright © 2020-2023  润新知