错误:在 C Prog 中,赋值从 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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还没有(至少在粘贴的代码中没有)
#include
定义strtok
函数的标头。在 C 中,尚未原型化的函数被假定返回int
。因此,我们将int
(函数结果)分配给char*
(token
的类型),而不进行强制转换。当然,我们不想要演员阵容。我们想要
#include
标头,以便编译器理解strtok
返回的内容。但如果有其他东西可以完成这项工作,我们也不想使用 strtok。它有许多不明显的限制。要获得强大的字符串解析,请尝试
sscanf
。You haven't (at least, not in the pasted code)
#include
d the header that defines thestrtok
function. In C, functions that haven't been prototyped yet are assumed to returnint
. Thus, we're assigning from anint
(function result) to achar*
(the type oftoken
) without a cast.We don't want a cast, of course. We want to
#include
the header, so that the compiler understands whatstrtok
returns.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, trysscanf
.我认为你的
char *number[3];
应该是char number[3];
,或者至少你应该为3个number<中的每一个分配空间/code> 指针。
I think your
char *number[3];
should bechar number[3];
, or at least you should allocate space for each of the 3number
pointers.