Question

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);
     }
Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top