/* 배열에 토큰을 저장하는 코드가 정확한지 확신 할 수 없습니다. 내 프로그램을 실행할 때마다 token[0]
을 내 변수와 비교하는 코드는 출력을 제공하지 않으며 할당 된 기능을 수행하지 않기 때문에 그렇습니다.문자열 토큰을 저장하고 내용을 배열에 저장 한 다음 해당 내용을 추가 비교에 사용하는 방법
는 그러므로 나는 나의 코딩에 대한 부정확 한 뭔가가 확신합니다. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
//variable declarations
const char *array[] = {"ax","bo","cf"};
char delim[]=" \n";
char* myline;
size_t max = 500;
char* token1;
char* token2[max];
int n = 0;
while(1) //loop always
{
printf("Enter an argument\n"); //asks for an input
getline (&myline, &max, stdin); //read the input/line
//for loop -- splits up the line into tokens
for(token1 = strtok(myline, " "); token1 != NULL; token1 = strtok(NULL, delim))
{
token2[n] = malloc(strlen(token1)+1); //allocate some space/memory to token2[n]
//save the token in an array by copying from token1 to token2
strcpy(token2[n],token1);
int m;
for(m = 0; m<sizeof(array);m++) //loop through the array elements for comparison
{
//compare array at index m with token at index 0 -- compare only first token with a specific variable
if(strcmp(token2[0], array[m]) == 0)
{
printf("equal");
}
}
}
free(token2[n]); //deallocate assigned memory
}
return(0);
}
코드의 유일한 문제점은 스타일이며'malloc'의 반환 값을 확인하지 못하는 것입니다. –
어디서 읽습니까? –
getline – Atinuke