Posted on 16th November 2009No Responses
Structures- Definition and Example

Structures
A structure is a collection of variables referenced under one name, providing a convenient means of keeping related information together. A structure declaration forms a template that may be used to create structure objects (that is, instances of a structure). The variables that make up the structure are called members. (Structure members are also commonly referred to as elements or fields).
Generally, all of the members of a structure are logically related. For example, the name and address information in a mailing list would normally be represented in a structure. The following code fragment shows how to declare a structure that defines the name and address fields. The keyword struct tells the compiler that a structure is being declared.

struct addr
{
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
};

Notice that the declaration is terminated by a semicolon. This is because a structure declaration is a statement. The type name of the structure is addr. As such, addr identifies this particular data structure and is its type specifier.
At this point, no variable has actually been created. Only the form of the data has been defined. When you define a structure, you are defining a compound variable type, not a variable. Not until you declare a variable of that type does one actually exist. In C, to declare a variable (i.e., a physical object) of type addr, write
struct addr addr_info;
This declares a variable of type addr called addr_info. In C++, you may use this shorter form.
addr addr_info;
As you can see, the keyword struct is not needed. In C++, once a structure has been declared, you may declare variables of its type using only its type name, without preceding it with the keyword struct.
The reason for this difference is that in C, a structure’s name does not define a complete type name. In fact, Standard C refers to a structure’s name as a tag. In C, you must precede the tag with the keyword struct when declaring variables. However, in C++, a structure’s name is a complete type name and may be used by itself to define variables. Keep in mind, however, that it is still perfectly legal to use the C-style declaration in a C++ program.
When a structure variable (such as addr_info) is declared, the compiler auto-matically allocates sufficient memory to accommodate all of its members.
addr_info appears in memory assuming 1-byte characters and 4-byte long integers.
You may also declare one or more structure variables when you declare a structure.
For example,
struct addr {
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
} addr_info, binfo, cinfo;
defines a structure type called addr and declares variables addr_info, binfo, and cinfo of that type. If you only need one structure variable, the structure type name is not needed. That means that

struct {
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
} addr_info;

declares one variable named addr_info as defined by the structure preceding it. The general form of a structure declaration is

struct struct-type-name
{
type member-name;
type member-name;
type member-name;
.
.
.
}
structure-variables;
where either struct-type-name or structure-variables may be omitted, but not both.

Accessing Structure Members
Individual members of a structure are accessed through the use of the. operator (usually called the dot operator). For example, the following code assigns the ZIP code 12345 to the
zip field of the structure variable addr_info declared earlier:
addr_info.zip = 12345;
The structure variable name followed by a period and the member name references that individual member. The general form for accessing a member of a structure is
structure-name.member-name
Therefore, to print the ZIP code on the screen, write
printf(”%d”, addr_info.zip);
This prints the ZIP code contained in the zip member of the structure variable addr_info.
In the same fashion, the character array addr_info.name can be used to call gets(), as shown here:
gets(addr_info.name);
This passes a character pointer to the start of name.
Since name is a character array, you can access the individual characters of addr_info.name
by indexing name. For example, you can print the contents of addr_info.name one character at a time by using the following code:
register int t;
for(t=0; addr_info.name[t]; ++t)
putchar(addr_info.name[t]);

Popularity: 1% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • BlinkList
  • blogmarks
  • BlogMemes Fr
  • BlogMemes Sp
  • Blogosphere News
  • blogtercimlap
  • co.mments
  • connotea
  • Current
  • Design Float
  • Diigo
  • DotNetKicks
  • DZone
  • eKudos
  • email
Comments
Leave a Response