Basics of Structures in C

By: Grenfel Emailed: 1642 times Printed: 2110 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces. An optional name called a structure tag may follow the word struct (as with point here). The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces.

The variables named in a structure are called members. A structure member or tag and an ordinary (i.e., non-member) variable can have the same name without conflict, since they can always be distinguished by context. Furthermore, the same member names may occur in different structures, although as a matter of style one would normally use the same names only for closely related objects.

Let us create a few structures suitable for graphics. The basic object is a point, which we will assume has an x coordinate and a y coordinate, both integers.

The two components can be placed in a structure declared like this:

   struct point {
       int x;
       int y;
   };

A struct declaration defines a type. The right brace that terminates the list of members may be followed by a list of variables, just as for any basic type. That is,

   struct { ... } x, y, z;
is syntactically analogous to
   int x, y, z;
in the sense that each statement declares x, y and z to be variables of the named type and causes space to be set aside for them.

A structure declaration that is not followed by a list of variables reserves no storage; it merely describes a template or shape of a structure. If the declaration is tagged, however, the tag can be used later in definitions of instances of the structure. For example, given the declaration of point above,

   struct point pt;
defines a variable pt which is a structure of type struct point. A structure can be initialized by following its definition with a list of initializers, each a constant expression, for the members:
   struct maxpt = { 320, 200 };
An automatic structure may also be initialized by assignment or by calling a function that returns a structure of the right type.

A member of a particular structure is referred to in an expression by a construction of the form

  structure-name.member

The structure member operator ``.'' connects the structure name and the member name. To print the coordinates of the point pt, for instance,

   printf("%d,%d", pt.x, pt.y);
or to compute the distance from the origin (0,0) to pt,
   double dist, sqrt(double);

   dist = sqrt((double)pt.x * pt.x + (double)pt.y * pt.y);
Structures can be nested. One representation of a rectangle is a pair of points that denote the diagonally opposite corners:

 

   struct rect {
       struct point pt1;
       struct point pt2;
   };
The rect structure contains two point structures. If we declare screen as
   struct rect screen;
then
   screen.pt1.x
refers to the x coordinate of the pt1 member of screen.

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Grenfel
Verifying a Signed JAR File example
Bitwise and Bit Shift Operators example in Java
JavaBeans Basic Concepts
The Differences Between Simple and Classic Tags in JSP
Bitwise Logical Operators in C
DispatchAction in Struts
union example program in C
Comparison operators in JSP
Basics of Structures in C
register Variables in C
Struts and Tiles - Steps to use Struts and Tiles
The if else statement in C
Character Counting sample program in C
Installing JSF
cin.ignore() in C++

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Using memset(), memcpy(), and memmove() in C
UNIX read and write system calls sample program in C
Open, Creat, Close, Unlink system calls sample program in C
Printing a simple histogram in C
lseek() sample program in C
Listing Files and Directories sample program in C
Character Arrays in C
Sum of the elements of an array in C
Find square and square root for a given number in C
Command-line Arguments in C
perror() Function - example program in C
goto and labels in C
Formatting with printf in C
scanf and sscanf sample program in C
Character Counting sample program in C
Most Emailed Articles (in C)
Multi-dimensional Arrays in C (Explained using date conversion program)
The for statement in C
Using Bit-field in C
Open, Creat, Close, Unlink system calls sample program in C
Formatting with printf in C
Printing a simple histogram in C
Basics of C
Arrays sample program in C
Character Arrays in C
else if statement in C
Initialization of Variables in C
File Inclusion in C
Pointers vs. Multi-dimensional Arrays in C
File read and write - sample program in C
UNIX read and write system calls sample program in C