2010-12-04 4 views
0

내가 위의 오류를 얻고있다 포인터를 역 참조 :오류 : 컴파일시 불완전 형

구조 : 오류의 원인이 될 수 무엇

struct connection_handlers 
{ 
    int m_fd; 

} 

struct connection_handlers ** _queue; 

int main() 
{ 

_queue = (struct connection_handlers **) malloc (3* sizeof (struct connection_handlers *)); //Allocating space for 3 struct pointers 

for (i=0;i<3;i++) 
{ 
    _queue[i]->m_fd=-1; 
}//Initializing to -1 

//..... 
//I assign this varaible to the file descriptor returned by accept and then 
//at some point of time i try to check the same variable and it gives compilatio error. 

for (i=0;i<3;i++) 
{ 
if (_queue[i]->m_fd!=-1) 
}//It give error at this line. 

} 

.

감사

+0

어떤 오류가 발생합니까? 더 많은 코드를 제공 할 수 있습니까? – bjskishore123

답변

1

_queue[i]connection_handlers *이다. 이 값을 -1과 비교할 수 없습니다.이 값은 int입니다. _queue[i]->m_fd을 확인하셨습니까?

+0

예. 나는 _queue [i] -> m_fd를 의미했습니다. 질문을 수정했습니다. 감사 칼. – sandeep

+0

편집 내용이 보이지 않습니다 ... –

4

C 및 C++ 모두이 질문에 태그를 추가 했으므로 여기에 C++의 문제점이 있습니다.

  • 구조체 선언이 ;
  • _queue을 종료하는 것은 엉망 업 유형으로 선언 할 필요 루프 카운터에 대한 암시 int을 사용하지 않는 사용자 캐스트 내부 struct을 넣지 마십시오
  • 마지막 루프가 누락되었습니다.

그것은 잘 컴파일됩니다.

#include <cstdlib> 

struct connection_handlers { 
    int m_fd; 
}; 

int main() { 
    connection_handlers** _queue = (connection_handlers**) malloc(3*sizeof (connection_handlers*)); 

    for (int i=0;i<3;i++) { 
    _queue[i]->m_fd=-1; 
    } 

    for (int i=0;i<3;i++) { 
    if (_queue[i]->m_fd!=-1) 
     ; // DOES NOTHING 
    } 
} 
+0

C에서는'struct'를 넣어야하는데,'connection_handlers'는 C에서 타입이 아닙니다. 그러나 C에서'malloc()'호출은'_queue = malloc (3 * sizeof * _queue);'어쨌든, 타입이 필요 없다. –