2014-03-30 3 views
1

Linux 커널에서 kobject에 대해 더 자세히 배우려고합니다. 그런 기능을 사용하는 모듈을 작성하려고 할 때 오류 및 경고 메시지가 표시됩니다. 관련 데이터 구조의 축소 된 버전과 해당 gcc의 오류 및 경고 메시지를 여기에 표시했습니다. 지정된 초기화 : GCC 경고 메시지 : 초기화 및 기타 경고 메시지 근처

$ gcc issue.c 
issue.c:30:1: error: initializer element is not constant 
} ; 
^ 
issue.c:30:1: error: (near initialization for ‘first.attr’) 
issue.c:34:1: error: initializer element is not constant 
}; 
^ 
issue.c:34:1: error: (near initialization for ‘second.attr’) 
issue.c:39:8: warning: initialization from incompatible pointer type [enabled by default] 
struct bin_attribute *my_bin_attrs[] = { &first_bin_attrs, &second_bin_attrs }; 
     ^
issue.c:39:8: warning: (near initialization for ‘my_bin_attrs[0]’) [enabled by default] 
issue.c:39:8: warning: initialization from incompatible pointer type [enabled by default] 
issue.c:39:8: warning: (near initialization for ‘my_bin_attrs[1]’) [enabled by default] 

그리고 샘플 코드 :

#include <stdio.h> 

struct attribute { 
    const char  *name; 
    unsigned short   mode; 
}; 

struct bin_attribute { 
    struct attribute attr; 
    unsigned int  size; 
    void   *private; 
}; 

struct attribute_group { 
    const char  *name; 
    struct attribute **attrs; 
    struct bin_attribute **bin_attrs; 
}; 

struct attribute first_attr = { 
    .name = "FIRST" 
}; 

struct attribute second_attr = { 
    .name = "SECOND" 
}; 

struct bin_attribute first = { 
    .attr = first_attr 
} ; 

struct bin_attribute second = { 
    .attr = second_attr 
}; 

struct bin_attribute *first_bin_attrs = &first; 
struct bin_attribute *second_bin_attrs = &second; 

struct bin_attribute *my_bin_attrs[] = { &first_bin_attrs, &second_bin_attrs }; 

int main(void) 
{ 
    struct attribute_group my_group = { 
     .name = "xyz", 
     .bin_attrs = my_bin_attrs, 
    }; 

    return 0; 
} 
+0

[const를 사용하여 변수를 초기화하려고 할 때 [Initializer 요소가 일정하지 않습니다.] 오류가 발생할 수 있습니다. "(http://stackoverflow.com/questions/3025050/error-initializer-element-is-not-constant-when- 변수를 초기화하려고 시도 함) – Dukeling

+0

오류 메시지가 표시되면 ** !! 항상 오류 메시지를 남기고 확인해야합니다 ** 오류 메시지는 [so] 질문을하기 전에 . – Dukeling

+0

[기타] 경고 메시지는 확실히 [so] 질문에 이상적이지 않습니다. 하나의 질문은 약 하나의 오류 (/ 질문) 여야합니다 (오류가 아니라 경고, 차이점이 있음). [so]에 게시 된 게시물은 많은 사용자에게 장기적인 가치를 제공하기위한 것입니다. 많은 것을 물어서 게시물을 오염 시키면 특정 게시물을 찾을 때이 게시물을 가로 챌 사람은 가볍습니다. 그렇다면 해당 게시물과 관련된 다른 모든 적용 불가능한 정보를 살펴 봐야합니다. 다른 오류. – Dukeling

답변

0

당신은 파일 범위 이니셜 라이저에서 초기화 같은 변수의 값을 사용할 수 없습니다.

당신은이 : 표현, first_attr를 사용하려고하기 때문에 두 번째는 '상수가 아닌 이니셜'오류를 생성

struct attribute first_attr = { 
    .name = "FIRST" 
}; 

… 

struct bin_attribute first = { 
    .attr = first_attr 
}; 

을, 그 컴파일 타임 상수가 아닙니다. 당신은 C++로 그것과 함께 떠날지도 모른다. C에서 할 수 없습니다.

변수의 주소를 사용할 수 있습니다. 이는 컴파일 타임 상수 (링크 타임에 해결됨)로 계산되지만 변수의 값으로 계산되지는 않습니다. 함수 안에서는 괜찮을 것이다.

struct bin_attribute을 다시 디자인하여 attr 구성원이 포인터가되거나 표시되는 초기화를 지체 시키십시오.

struct bin_attribute *first_bin_attrs = &first; 
struct bin_attribute *second_bin_attrs = &second; 

struct bin_attribute *my_bin_attrs[] = { &first_bin_attrs, &second_bin_attrs }; 

&first_bin_attrs의 유형 struct bin_attribute **이므로 초기화 값의 포인터 중 하나 너무 많은 단계가있다 :

에러들의 제 2 세트는 때문이다. &을 제거하면 유형은 올바르지 만 '비 상수 초기화 프로그램'문제점으로 되돌아갑니다. my_bin_attrs 유형에 *을 추가하면 Three Star Programmer이 될 위험이 있으므로 my_bin_attrs을 사용하는 코드를 수정해야합니다.

struct bin_attribute *first_bin_attrs = &first; 
struct bin_attribute *second_bin_attrs = &second; 

struct bin_attribute *my_bin_attrs[] = { &first, &second }; 

하지만 그건 만족 여부 것은 당신이 달성하려고했는지에 따라 달라집니다

간단한 수정 프로그램입니다.