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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
용감한 바로 가기 및 우분투 응용 프로그램안녕하세요 여러분, 이 기사에서는 모든 사이트에서 pwa를 생성하고 실행기 응용 프로그램으로 추가하는 방법을 설명하고 싶습니다. 일부 웹사이트는 PWA로 설치를 허용하지 않지만 유사한 애플리케이션을 원합니다. 1. ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.