General syntax of structure in C or C++:
struct structure_tag_name{
type variable_name1;
:
:
type variable_nameN;
} structure_variables;
sample structure definition:
struct address{
char name[30];
char street[30];
char zip[4];
};
or
struct {
char name[30];
char street[30];
char zip[4];
} address_info;
The difference in the two can be possible but not the same declaration may miss both the structure name or variables.
to assign or access the structure element:
strcpy(address_info.zip,"9500");
printf("%s",address_info.zip);
on the screen, you will see:
9500
See the complete sample program:
#include stdio.h /*enclose this library in <> */
main()
{
struct my_info{
char name[30];
float height;
float weight;
};
strcpy(my_info.name,"Rosilie");
my_info.height=64.0;
my_info.weight=48.0;
printf("\nHello %s",my_info.name);
printf("\nYou are %f inches tall",my_info.height);
printf("\nYou weigh %s kilograms",my_info.weight);
getche();
}
What is the output of the code above?
Translate this in C++.
1 comment:
I already forgot those computer programming languages, structures, syntax, etc. This blog reminds me of our college memories. :-) MarkFe
daily photos
Post a Comment