나는 캐릭터 라인 에 (공간 전에 첫 번째 단어를) 하위 문자열 얻을하는 방법 .... 내 코드를 작성하려고 해요 :C에서 자주 사용하기 위해 char 포인터를 초기화하는 방법은 무엇입니까?
char* getCommand(char* commandLine)
{
int index = 0;
char* command = {0};
command = malloc(1000);
printf("%d\n",(int)strlen(command));
printf("%s\n", command);
while(commandLine[index]!=' ')
{
command[index]=commandLine[index];
index++;
}
return (char *)command;
}
while(){
printf("%s\n",getCommand(cmd)); // cmd is char cmd[MAX_CMD_LENGTH];
}
잘 작동하지만 나에게 오류 제공합니다
을malloc.c : 2372 : sysmalloc : 어설 션`(old_top == ((mcharptr) ((char *) & (av) -> bins [((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd))))) & & old_size == 0) || ((unsigned long) (old_size)> = (unsigned long) ((__ builtin_offsetof (struct malloc_chunk, fd_nextsize)) + ((2 * (sizeof (size_t)) -1)) & ~ (old_top) -> 크기 & 0x1) & & & ((부호없는 길이) old_end & 페이지 마스크 == 0) '실패했습니다. 변수 명령이 지정되지 않은 값을 가지고 있기 때문에 중단됨 (덤프 코어)
'strlen (NULL)'-> 충돌 가능성이 높습니다 ... – FatalError
많은 문제가 여기에 있습니다. 데이터를 명령에 복사하는 중입니다.이 명령에는 할당 된 저장 영역이 없습니다. 포인터와 메모리 할당에 대한 더 많은 독서를 C에서 할 수 있습니다. – OldProgrammer
죄송합니다. NULL이 없으며 편집했습니다 ... 문제가 있지만 괜찮습니다. –