Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
C - Basics of structure       Share
2007-09-02 |  RatheeshTR  | Viewed: 454  |    0

Structure:
                A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for  convenient handling ( Structures   are called "records" in some languages, notably pascal ). Structures help to organsize complicated data, particularlyin large programs because  they permit a group of related variables  to be treated as a unit instead of as separate entities.

syntax:
                        struct struct_name  {
                                  datatype    var_name1;
                                             -----            
                                            -------
                        };

                        declaration:
                                   
                            struct struct_name struct_member_name;

Defining a Structure

            A structure type is usually defined near to the start of a file using a typedef statement. typedef defines and names a new type, allowing its use throughout the program. typedefs usually occur just after the #define and #include statements in a file.

Here is an example structure definition.

                    typedef struct {
char name[64]; char course[128]; int age; int year;
} student;
This defines a new type student variables of type student can be declared as follows.

              student st_rec;

Notice how similar this is to declaring an int or float.
The variable name is st_rec, it has members called name, course, age and year.

Basics of structures.
                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.

y
|
-
|
-                * (4,3)
|
-       
|
-----+------+--------+-------- x
(0,0)
The two components can be placed in a structure declared like this

    struct point   {
            int   x;
            int   y;
            };


Structure can be declared as

            struct point pt;
  
The keyword struct introduces a structure declaration, Which is a list of declaratons 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.

 Accessing Members of a Structure

Each member of a structure can be used just like a normal variable, but its name will be a bit longer. To return to the examples above, member name of structure st_rec will behave just like a normal array of char, however we refer to it by the name


  st_rec.name

Here the dot is an operator which selects a member from a structure.

Where we have a pointer to a structure we could dereference the pointer and then use dot as a member selector. This method is a little clumsy to type. Since selecting a member from a structure pointer happens frequently, it has its own operator -> which acts as follows. Assume that st_ptr is a pointer to a structure of type student We would refer to the name member as


  st_ptr -> name

For example to assign a value, we can enter:
example. 1
struct x {
                int a;
                int b;
                int c;
};   main()   
{
     
struct x z;           
z.a = 10;      
z.b = 20;      
z.c = 30;   
}
example. 2
struct x   
{
     
int a;      
int b;      
int c;   
};main()   
{
     
struct x z;          
z.a = 10;      
z.a++;
printf(" first member is %d \n", z.a);     
}
example

1.Struct example


Latest topics
C - Arrays of structures  Viewed: 285
C - structures and functions  Viewed: 292
tcpdump by host name or ip  Viewed: 292
C - pointers and Arrays  Viewed: 295
C - File Access  Viewed: 295
C - For loop statements  Viewed: 296
C - pointers to pointers  Viewed: 296
C - Register variables  Viewed: 297
C - Static variables  Viewed: 299
C - Pass-by-reference parameters  Viewed: 300

Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
Send Mail: ratheesh



Related topics
C - Functions and program structure, Programmed pass-by-reference via pointers, Pass-by-reference parameters
C - Pass-by-reference parameters
C - pass-by-reference via pointers

Related References
c - swap num, pass-by-reference parameters
c - pass-by-reference via pointers

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us