Trouble filling a structure from a file...receiving error message incompatible types in assignment

Posted on 2004-12-01
Last Modified: 2010-04-15
I am trying to fill the structure STUREC and am experiencing problems with my fscanf statement.  Upon compilation I receive the following message: proj6.c:83: incompatible types in assignment.  Can someone help me determine the source of my error?

My code is as follows:

//This program is designed to

#include <stdio.h>
#include <string.h>
#define STR_SIZE 25
#define ARR_SIZE 10
#define TRUE 1
#define FALSE 0

typedef struct
   char *month;
   int date;
   int year;

typedef struct
   char fName[STR_SIZE];
   char lName[STR_SIZE];
   int gradeLevel;
   char ssn[STR_SIZE];
   DATE birthDate;

typedef enum {false, true} BOOLEAN;

BOOLEAN openFile(int argc, char *argv[], FILE **fpIn);
int fillArray(FILE *fpIn, STUREC arr[]);
int getMonth(char *temp);
void sortArray(STUREC arr[], int numEls);
void swap(STUREC *stu1, STUREC *stu2);
void printArray(STUREC arr[], int numEls);
void closeFile(FILE *fpIn);

// do not change this function
int main(int argc, char *argv[])
   int numEls;
   FILE *fpIn;

   if (openFile(argc, argv, &fpIn))
      numEls = fillArray(fpIn, arr);
      printArray(arr, numEls);
      sortArray(arr, numEls);
      printArray(arr, numEls);

   return 0;

BOOLEAN openFile(int argc, char *argv[], FILE **fpIn)
   BOOLEAN success=false;
   if (argc >= 2)
      *fpIn = fopen(argv[1], "r");
      if (*fpIn != NULL)
         success = true;
         printf("\nERROR: Unable to open file for reading.\n");
      printf("\nERROR: Not enough command line arguments.\n\n");
   return success;

int fillArray (FILE *fpIn, STUREC arr [])
   int i;
   STUREC stu;
   for (i=0; i < ARR_SIZE; i++)
   arr [i] = fscanf (fpIn, "%s%s%d%s", stu.fName, stu.lName,
                     &stu.gradeLevel, stu.ssn);
Question by:csunnypl2
    LVL 55

    Accepted Solution

    the problem is not fscanf
    fscanf returns an integer representing the numbers of fields it has correctly parsed, in this case could be 4
    You are trying to assign 4 to arr[i]
    I guess you want to copy contents of 'stu' to arr[i], so correct code would be:

    int fillArray (FILE *fpIn, STUREC arr [])
       int i;
       STUREC stu;
       for (i=0; i < ARR_SIZE; i++)
       fscanf (fpIn, "%s%s%d%s", stu.fName, stu.lName,  &stu.gradeLevel, stu.ssn);
       arr[i] = stu;
    /* alternative to previous line:
       memcpy(&arr[i], &stu, sizeof(stu));

    LVL 3

    Expert Comment

    Your problem's here:

    arr [i] = fscanf (fpIn, "%s%s%d%s", stu.fName, stu.lName, &stu.gradeLevel, stu.ssn);

    fscanf returns an int equal to the number of items it's read.  This line tries to assign that int value to an element in your STUREC array, arr.  This is what you want to do, I think:

    fscanf (fpIn, "%s%s%d%s", arr[i].fName, arr[i].lName, &(arr[i].gradeLevel), arr[i].ssn);

    And remove STUREC stu from your fillArray method (you don't use it anymore).


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now