new*_*bie 0 c pointers casting strtok
每当我运行程序"Assignment在没有强制转换的情况下从Integer发出指针"时,我都会收到此错误.我的代码写在下面....请帮助......谢谢
struct student {
char studentID[6];
char name[31];
char course [6];
};
struct student *array[MAX];
struct student dummy;
int recordCtr=0;
int read(){
FILE *stream = NULL;
int ctr;
char linebuffer[45];
char delims[]=", ";
char *number[3];
char *token = NULL;
stream = fopen("student.txt", "rt");
if (stream == NULL) stream = fopen("student.txt", "wt");
else {
printf("\nReading the student list directory. Wait a moment please...");
while(!feof(stream)){
array[recordCtr]=(struct student*)malloc(sizeof(struct student));
while(!feof(stream)) {
fgets(linebuffer, 46, stream);
token = strtok(linebuffer, delims); //This is where the error appears
ctr=0;
while(token != NULL){
strcpy(number[ctr], linebuffer);
token = strtok(NULL, delims); //This is where the error appears
ctr++;
}
strcpy(array[recordCtr] -> studentID,number[0]);
strcpy(array[recordCtr] -> name,number[1]);
strcpy(array[recordCtr] -> course,number[2]);
}
recordCtr++;
}
recordCtr--;
fclose(stream);
}
Run Code Online (Sandbox Code Playgroud)
您没有(至少在粘贴的代码中)#included定义strtok函数的标头.在C中,假定还没有原型的函数返回int.因此,我们在没有强制转换的情况下从int(函数结果)分配到char*(类型token).
当然,我们不想要演员.我们想要#include标题,以便编译器理解strtok返回的内容.
但是,strtok如果还有其他任何可以完成工作的话,我们也不会真的想要使用它们.它有许多不明显的限制.对于健壮的字符串解析,请尝试sscanf.