Error: Assignment makes pointer from Integer without a cast… in C Prog

匆匆过客 提交于 2019-12-02 18:14:40

问题


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

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/4330682/error-assignment-makes-pointer-from-integer-without-a-cast-in-c-prog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!