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


i compiling below c , .h codes in gcc terminal (centos linux) atm project received below errors. please new programming.

validate_acc.h #ifndef _validate_acc_ #define _validate_acc_  struct validate_acc { int user_acc_try, = 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 account number: ");                scanf("%d", &va.user_acc_try);                 if(va.user_acc_try != account_number){                                    printf("you entered 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 validate_acc.c:2:0: validate_acc.h:5:23: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or   ‘__attribute__’ before ‘=’ token   int user_acc_try, = 0;                    ^ validate_acc.c:6:20: error: expected ‘)’ before ‘va’  void (validate_acc va) 

structure type declarations not support initializers, such trying provide. moreover, intializers structure objects expressed quite differently trying do. struct declaration should have form:

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

that declares type, not (in itself) object associated storage initialize. that's why can declare type alias have done:

typedef struct validate_acc validate_acc; 

if want perform initialization of objects of type must on per-object basis, so:

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

or, using typedefed alias:

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

perhaps idea provide default values structure's members, c not have such feature. can create shortcut initialization macro, however, such as

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

. do

struct validate_acc validate_object = validate_acc_defaults; 

that doesn't shorten code in case, make clearer. supposing put macro definition in same header structure declaration, provides central place can change default initialization values.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -