user3578911
user3578911

Reputation: 1

sizeof error in a struct

I keep getting an error that says error:

invalid application of 'sizeof' to incomplete type 'struct customer' 

I don't know how to fix the problem. Can anyone suggest ways for me to alleviate this problem?

typedef struct
{
  int acc_number;
  char first_name[30];
  char last_name[30];
  char address [50];``
  int contact_info;
  float acc_bal;
} customer ;

struct employee
{
  int emp_id;
  char first_name[30];
  char last_name[30];
  char address [50];
  char job_title [30];
  int contact_info;
};

int main()
{

  FILE *myPtr;
  customer customer = { 0.0 };

  if ( ( myPtr = fopen( "credit.dat", "r+" ) ) == NULL )
    printf( "File could not be opened.\n" );
  else {
    printf( "Enter account number"
          " ( 1 to 100, 0 to end input )\n? " );
    scanf( "%d", &customer.acc_number);

    while ( customer.acc_number != 0 ) {
      printf( "Enter lastname, firstname, balance\n? " );
      fscanf( stdin, "%s%s%f", customer.last_name,
              customer.first_name, &customer.acc_bal );
      fseek( myPtr, ( customer.acc_number - 1 ) *
             sizeof (struct   customer ), SEEK_SET );
      fwrite( &customer, sizeof ( struct customer ), 1,
             myPtr );
      printf( "Enter account number\n? " );
      scanf( "%d", &customer.acc_number );

    }

    fclose(myPtr);
}

return 0;

}

Upvotes: 0

Views: 630

Answers (2)

chux
chux

Reputation: 153458

As @pmg well points out why the below fails: code does not have a struct customer.

fwrite( &customer, sizeof ( struct customer ), 1, myPtr );

The declaration of the variable customer, though legal uses "customer" in 2 different ways.

customer customer = { 0.0 };

Suggest using the sizeof with the variable name and not the type name. Also use distinguishing type and variable names.

customer current_customer = { 0.0 };
fwrite( &current_customer, sizeof current_customer, 1, myPtr );

Even if one prefers to not usetypedef and use struct when declaring variables, still recommend using sizeof with the variable name and not the type.

struct customer {
  int acc_number;
  ...
  float acc_bal;
};

struct customer current_customer = { 0.0 };
fwrite( &current_customer, sizeof current_customer, 1, myPtr );

Upvotes: 2

pmg
pmg

Reputation: 108988

You do not have a struct customer.

You have a struct with no tag with an alias of costumer.

Try

typedef struct costumer { /* ... */ } costumer;
//             <--tag->               <-alias->
//      <--type name-->
//      <-------------type---------->

Or, maybe even better, leave typedef out of your code.

Upvotes: 4

Related Questions