错误:在C Prog中,赋值使Integer的指针不进行强制转换... [英] Error: Assignment makes pointer from Integer without a cast... in C Prog
问题描述
每当我运行程序赋值使Integer指针不进行强制转换"时,都会出现此错误.我的代码写在下面....请帮助...谢谢
I get this error whenever i run the program " Assignment makes pointer from Integer without a cast". My code is written below.... Please help... Thankx
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);
}
推荐答案
您还没有(至少不是在粘贴的代码中)#include
d定义了strtok
函数的标头.在C语言中,尚未原型化的函数被假定为返回int
.因此,我们将int
(函数结果)分配给char*
(类型为token
),而无需进行强制转换.
You haven't (at least, not in the pasted code) #include
d the header that defines the strtok
function. In C, functions that haven't been prototyped yet are assumed to return int
. Thus, we're assigning from an int
(function result) to a char*
(the type of token
) without a cast.
当然,我们不需要演员.我们想要#include
标头,以便编译器理解strtok
返回的内容.
We don't want a cast, of course. We want to #include
the header, so that the compiler understands what strtok
returns.
但是,如果还有其他功能可以使用,我们也不想使用strtok
.它具有许多显而易见的限制.要进行健壮的字符串解析,请尝试sscanf
.
But we also don't really want to use strtok
if there's anything else that will do the job. It has numerous limitations that aren't obvious. For robust string parsing, try sscanf
.
这篇关于错误:在C Prog中,赋值使Integer的指针不进行强制转换...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!