Can you make a c struct from an array of specific length? -
i'm working on c program generates doubly linked list of "records".
i've got structs prototyped follows:
struct custrec(char[20] name, char[50] address, char[20] city, char[2] stateabbreviation, int zipcode, float balance); struct linkedrec(custrec storedrec, custrec* nextrec, custrec* prevrec); specifically, custrec struct, valid use char[20] name instead of char[] name?
by "valid", mean -- i'm trying limit "name" field 19 characters (+ null terminator). should worry length elsewhere , make struct accept char arrays of length?
this syntax incorrect (i misread question @ first because function declarations, not structure definitions):
struct custrec(char[20] name, char[50] address, char[20] city, char[2] stateabbreviation, int zipcode, float balance); struct linkedrec(custrec storedrec, custrec* nextrec, custrec* prevrec); you meant:
typedef struct custrec { char name[20]; char address[50]; char city[20]; char stateabbreviation[2]; int zipcode; float balance; } custrec; // necessary because question c, not c++ typedef struct linkedrec { custrec storedrec; custrec *nextrec; custrec *prevrec; } linkedrec; i note in us, state abbreviations 2 characters, need use char state[3] allow terminating null.
this syntactically valid. when copy data fields of custrec, need careful ensure copying not overflow bounds. compiler not enforce lengths.
you need check, therefore, inside function strings passed not exceed limits expect.
if prefer not impose limits on lengths, string structure members can made char * , can dynamically allocate memory arbitrary length strings. not idea state abbreviations; there want enforce '2 characters plus '\0'' limit.
Comments
Post a Comment