2014-1-5_링크 ux 커 널 학습 (1)C 언어 기초

리 눅 스 의 커 널 모듈 화 프로 그래 밍 을 배우 고 참고 자료 중 몇 가지 기본 적 인 문법 이 낯 설 습 니 다.
1. 구조 체 초기 화
"문자 장치 구동" 보기 (http://www.tldp.org/LDP/lkmpg/2.6/html/x569.html) 안에 이런 코드 가 있다.
static struct file_operations fops = {
   .read = device_read,
   .write = device_write,
   .open = device_open,
   .release = device_release
};
예전 에 C 언어 를 배 울 때 struct 의 이런 초기 화 방식 을 본 적 이 없다.이것 은 사실 C 언어의 새로운 표준 이다.Struct 는 모두 세 가지 초기 화 방식 이 있 습 니 다.
int main(void){
        struct sct{
                int age;
                char *name;
        };
        // way 1
        struct sct s1={1,"aa"};
        // way 2
        struct sct s2={
                .age=1,
                .name="bb"
        };
        //way 3
        struct sct s3={
                age:1,
                name:"cc"
        };
        printf("%d,%s
",s1.age,s1.name); printf("%d,%s
",s3.age,s3.name); printf("%d,%s
",s2.age,s2.name); return 0; }

2. 함수 포인터
"문자 장치 구동" (http://www.tldp.org/LDP/lkmpg/2.6/html/x569.html) 안에 구조 체 의 용법 이 비교적 복잡 하 다.
static struct file_operations fops = {
   .read = device_read,
   .write = device_write,
   .open = device_open,
   .release = device_release
};
file_operations 구조 체 가 들 어 오 는 4 개의 변 수 는 함수 명 이지 일반적인 변수 가 아 닙 니 다.이것 은 지향 함수 의 지침 에 관 한 지식 과 관련된다.포인터 자체 도 변수 자체 로 서 메모리 공간 을 차지 하고 메모리 공간 도 데 이 터 를 저장 할 수 있 으 며 이 데 이 터 를 읽 을 수 있 습 니 다.인용 으로 사용 할 때 * p 는 먼저 주소 에 저 장 된 데 이 터 를 읽 고 주소 로 보고 이 주소 에서 데 이 터 를 읽 어 결과 로 삼 아야 합 니 다.(http://blog.chinaunix.net/uid-23629988-id-3084750.html)
단순 시 뮬 레이 션 함수 포인터 의 사용법:
#include<stdio.h>
struct file_operations{
        void (*open)();
        void (*close)();
};
void file_open(){
        printf("file is open
"); } void file_close(){ printf("file is close
"); } void register_dev(struct file_operations *fops){ fops->open(); fops->close(); } int main(void){ struct file_operations fops={ .open=file_open, .close=file_close }; register_dev(&fops); return 0; }

좋은 웹페이지 즐겨찾기