prebek
asked on
structure printing
I'm trying to declare, initialize, and print the contents of a structure. The last printf attempts to print a structure within a structure. Is my info initialized properly and will it print properly?
Struct customer
{
int account;
char street[60];
char citystate[20];
int zip;
float balance;
int creditlimit;
struct customer * account;
};
Struct customer = {
3303101122,
"Jones Street",
"Springfield MA",
01104,
5310.51,
int 10000,
struct customer * account = 0
};
printf("\n",customer.account);
printf("\n", customer.street);
printf("\n", customer.citystate);
printf("\n", customer.zip);
printf("\n", customer.balance);
printf("\n", customer.creditlimit);
printf("\n", &customer[6]);
Also, a suggestion based on experience.
Use strings / char arrays for zipcode. Leading zeros will be preserved with a string, but not with a plain int.
Same goes for account code, if your accounts may have leading zeroes, then you will lose them unless using strings.
Use strings / char arrays for zipcode. Leading zeros will be preserved with a string, but not with a plain int.
Same goes for account code, if your accounts may have leading zeroes, then you will lose them unless using strings.
Also printf supports multiple variables in the format.
printf("%d\n%s\n", customer.account, customer.street);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For example, you can print:
%s - string / const char array / const char *
%d - integer
%f - float
%c - single character
See below, I have modified your statements with specifiers according to the data types you declared. However, to print an array of structs or nested structs, you must write a routine to print each member, can't print the whole thing.
Open in new window