Atidivya Patra
Atidivya Patra

Reputation: 15

error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token & error: expected ‘)’ before ‘va’

I am compiling the below c and .h codes in GCC terminal (centos linux) for an ATM project received the below errors. Please help as I am new to programming.

validate_acc.h
#ifndef _VALIDATE_ACC_
#define _VALIDATE_ACC_

struct validate_acc {
int user_acc_try, i = 0;
 int user_has_not_entered_right_acc = 1;
 int retries = 3;
  };

 typedef struct validate_acc Validate_acc;




  #endif

===============================================

validate_acc.c

#include<stdio.h>
 #include "validate_acc.h"

  extern int account_number;

  void (Validate_acc va)
{


va.user_acc_try, va.i = 0;
va.user_has_not_entered_right_acc = 1;
va.retries = 3;



 while(va.retries > 0 && va.user_has_not_entered_right_acc == 1){
               printf("\nPlease enter your account number: ");
               scanf("%d", &va.user_acc_try);

               if(va.user_acc_try != account_number){
                                   printf("You entered the wrong     account      number\n");
                               va.retries--;
                               }
               else{
               va.user_has_not_entered_right_acc = 0;
               }
               }
 }

====================errors

[linux@localhost Assignment1]$ gcc -c validate_acc.c
In file included from validate_acc.c:2:0:
validate_acc.h:5:23: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or   ‘__attribute__’ before ‘=’ token
  int user_acc_try, i = 0;
                   ^
validate_acc.c:6:20: error: expected ‘)’ before ‘va’
 void (Validate_acc va)

Upvotes: 1

Views: 13231

Answers (1)

John Bollinger
John Bollinger

Reputation: 180181

Structure type declarations do not support initializers, such as you are trying to provide. Moreover, intializers for structure objects are expressed quite differently than you are trying to do. Your struct declaration should have this form:

struct validate_acc {
    int user_acc_try;
    int i;
    int user_has_not_entered_right_acc;
    int retries;
};

That declares a type, not (in itself) any object with associated storage that you could initialize. That's why you can declare a type alias for it as you have done:

typedef struct validate_acc Validate_acc;

If you want to perform initialization of objects of that type then you must do it on a per-object basis, like so:

struct validate_acc validate_object = { 0, 0, 1, 3 };

or, using the typedefed alias:

validate_acc validate_object = { 0, 0, 1, 3 };

Perhaps your idea was to provide default values for your structure's members, but C does not have such a feature. You can create a shortcut initialization macro, however, such as

#define VALIDATE_ACC_DEFAULTS { 0, 0, 1, 3 }

. You could then do

struct validate_acc validate_object = VALIDATE_ACC_DEFAULTS;

That doesn't shorten the code in this case, but it does make it clearer. Supposing you put the macro definition in the same header is the structure declaration, it also provides a central place where you can change the default initialization values.

Upvotes: 5

Related Questions