Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

structure printing

Posted on 2009-04-09
4
Medium Priority
?
752 Views
Last Modified: 2012-05-06
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]);

Open in new window

0
Comment
Question by:prebek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24113162
printf() has format specifiers for basic data types, but those do NOT include structures.

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.


printf("%d\n",customer.account);
printf("%s\n", customer.street);
printf("%s\n", customer.citystate);
printf("%d\n", customer.zip);
printf("%f\n", customer.balance);
printf("%d\n", customer.creditlimit);
// printf("\n", &customer[6]); // ILLEGAL, CANT PRINT A STRUCT OR ARRAY OF STRUCTS

Open in new window

0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24113168
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.
 
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24113182
Also printf supports multiple variables in the format.
printf("%d\n%s\n", customer.account, customer.street);

Open in new window

0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 24113518
>> Leading zeros will be preserved with a string, but not with a plain int.

Not that a leading zero for an int literal (like 01104 for example) actually means that the value should be interpreted as an octal value instead of the normal decimal value. So keeping the leading 0 there actually changes the result from what you'd expect.


Also, the name of the struct type you defined is 'struct customer' (note the lowercase s of struct - keywords in C are all lowercase only). So, when instantiating that struct, you use the complete type name followed by the name of the instance. For example :

        struct customer myCustInstance;

optionally with an initialization between {}'s after it. May I suggest using an uppercase first character for the type names (ie. Customer instead of customer), and a lowercase first character for variable names (like myCustInstance) ? It makes it very easy to distinguish between types and variables.


Finally, the way you initialized the struct looks mostly ok, except for the last two fields where you forgot to leave out the field information - you should just put the desired value, so :

        10000,
        0

instead of :

>>    int 10000,
>>    struct customer * account = 0
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question