알고리즘/정리 및 오류 해결 방법

Cannot initialize a variable of type 'int' with an rvalue of type 'void *' 에러 해결법

고수트 2018. 12. 9. 16:58
반응형

변수 선언 오류

Cannot initialize a variable of type 'int' with an rvalue of type 'void *' 와 같은 에러는 흔히

malloc() 과 같은 함수를 통해 변수를 선언할때 자주 발생한다.

ex)

> int temp = malloc(sizeof(int));

> Cannot initialize a variable of type 'int' with an rvalue of type 'void *'


이러한 경우의 이유는 

malloc() 함수로 선언되는 경우에는 void* 형식으로 선언이 되어 int 변수에 할당할 수가 없기 떄문에 발생한다. 

따라서 (int*) 와 같은 식으로 변환을 시킨 뒤 선언해 주면 할당된다.

> int* temp = (int*)malloc(sizeof(int));


반응형