Manuel González Manuel González - 27 days ago 16
C Question

Error ocurred while trying to compile

Two errors appear when I try to compile this:

typedef struct {

int account_number;
float balance;


}accounts[2];


typedef struct{
char name[40];
accounts[2]


}client[3];


The errors are :

prac5_2.c:16:8: error: expected identifier or ‘(’ before ‘[’ token
accounts[2]

prac5_2.c:18:2: error: expected ‘;’ before ‘client’
}client[3];


And I don´t know what I have to write for it to be correct

Answer

The line accounts[2] in the second typedef is causing the error.

Modify the code to match the specfication. For example, you can delete the line or add a variable name and a semicolon to the line in order to get lid of the error.

typedef struct{
    char name[40];
    /* accounts[2] */
    accounts hoge[2];
}client[3];

Note that your code declares accounts as a type which is 2-element array of the struct having account_number and balance. accounts hoge[2]; will be 2x2 array of the struct. Like that, client will be a type which is 3-element array.


If you want to just create types that are structs, not arrays of structs, you can write like this:

typedef struct {
    int account_number;
    float balance;
}accounts_t;
accounts_t accounts[2];


typedef struct{
    char name[40];
    accounts_t accounts[2];
}client_t;
client_t client[3];

Note that in this code, accounts in the strucct client_t is 1-dimention array of 2 elements, not 2x2 array.