总结

1、

#include<stdio.h>
#include<stdlib.h>
struct student
{
    char gender;
    int id;
    int age;
};
int main()
{
    struct student s;
    printf("%d\n",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\n");
    }
    v->p = (struct page *)malloc(sizeof(struct page));
    if(!v->p)
    {
        printf("error\n");
    }
    if(v->p)
    {
        printf("111\n");
    }
    else
    {
        printf("222\n");
    }
    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,所有文件应该包含在一个文件夹中

转载于:https://www.cnblogs.com/scu-cjx/p/6878512.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值