0

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);
     }
4

2 回答 2

7

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

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

strtok但是,如果还有其他东西可以完成这项工作,我们也真的不想使用。它有许多不明显的限制。对于健壮的字符串解析,请尝试sscanf.

于 2010-12-02T00:58:23.900 回答
1

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

于 2010-12-02T00:59:02.633 回答