2012-01-31 4 views
2

strtok 기능을 모방하려고했지만 세그먼트 화 오류가 발생했습니다. 제발 도와주세요.이 코드의 잘못된 점은 무엇입니까?

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

int main(int argc, char argv[]) 
{ 
    int i=0; 
    char c[]="get the hell out of here"; 
    char *p; 
    char *temp=(char *)malloc(100); 
    while(c[i]!='\0') 
    { 
     if(c[i]!=' ') 
     { 
      *temp=c[i]; 
      temp++; 
      i++; 
     } 
     else 
     { 
      *temp='\0'; 
      printf("printing tokenn"); 
      puts(temp); 
      i++; 
      temp=""; 
     } 
    } 
    return 0; 
} 
+2

* void main * !!!! – dreamlax

+1

@dreamlax void main은 실제로 괜찮습니다. Shashank는'# include '를 코드로 옮겨서 제대로 포맷 할 수 있습니까? 그리고'temp = "";'줄로 무엇을 의미합니까? – Vyktor

+0

@Vyktor :'void main'이 실제로 작동 할 수는 있지만 합법적이지는 않습니다. – jamesdlin

답변

8
temp=""; 

이 고장에 당신이 그것을 통해 수정하려고 다음 번을 선도 불가능한 메모리를 가리 키도록 temp가 발생합니다

여기 내 코드입니다. tempmalloc에서 얻은 값으로 복원하려고했으나 저장하지 않았습니다.

관련 문제