错误:在 C Prog 中,赋值从 Integer 生成指针而不进行强制转换...

发布于 2024-10-05 16:46:57 字数 1528 浏览 5 评论 0原文

每当我运行程序“赋值使指针来自整数而不进行强制转换”时,我都会收到此错误。我的代码写在下面...请帮助...谢谢

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芸娘子的小脾气 2024-10-12 16:46:57

您还没有(至少在粘贴的代码中没有)#include定义 strtok 函数的标头。在 C 中,尚未原型化的函数被假定返回 int。因此,我们将 int (函数结果)分配给 char*token 的类型),而不进行强制转换。

当然,我们不想要演员阵容。我们想要#include标头,以便编译器理解strtok返回的内容。

但如果有其他东西可以完成这项工作,我们也不想使用 strtok。它有许多不明显的限制。要获得强大的字符串解析,请尝试 sscanf

You haven't (at least, not in the pasted code) #included 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.

We don't want a cast, of course. We want to #include the header, so that the compiler understands what strtok 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, try sscanf.

兲鉂ぱ嘚淚 2024-10-12 16:46:57

我认为你的char *number[3];应该是char number[3];,或者至少你应该为3个number<中的每一个分配空间/code> 指针。

I think yourchar *number[3]; should be char number[3];, or at least you should allocate space for each of the 3 number pointers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文