Structures in C

Posted by Kalyan Kurasala  |  No comments

The C language gives 5 ways to create a custom datatype:
  1. The Structure, which is a group of variables under one name and is called an aggregate datatype.
  2. The union, which enables the same piece of memory to be defined as two or more different types of variables.
  3. The bit-field, which is special type of structure or union element that allows easy access to individual bits.
  4. The enumeration, which is a list of named integer constants.
  5. The typedef keyword, which defines a new name for an existing type.
Structures: A structure is a collection of variables referenced under one name, providing a convenient means of keeping related information together. The structure declaration forms a template that can be used to create structure objects (i.e., instances of a structure). These variables are called members of that structure or structure members.
The general form of structure declaration is as follows
                                struct tag
                                 {
                                   type member_name;
                                   type member_name;
                                   ...
                                   ...
                                 }structure_variables;
The keyword 'struct' is used to declare a structure.
eg.,                             struct addr
{
char name[30];
char street[40];
char city[20];
char state[10];
unsigned long int zip;
};
Notice that the declaration is terminated by a semicolon. This is because a structure declaration also a statement. Also, the structure tag addr identifies this particular data structure and is its type specifier.
In the above example, no variable has been declared or created. Only the form of data been defined.
If you want to declare a variable for this structure, then write as:
struct addr sinfo;
When a structure variable is declared, the compiler automatically allocates sufficient memory to accommodate all of its members.
You can also declare one or more objects when you declare a structure. For example,
struct addr
{
char name[30];
char street[40];
char city[20];
char state[10];
unsigned long int zip;
}sinfo,ainfo;
Note: If you only need one structure variable, then there is no need of structure tag.
Accessing Structure members: Individual members of a structure are accessed through the use of '.'(dot) operator.
For example, the following statement assign the zip code to the zip field of the structure variable 'sinfo' declared earlier:
sinfo.zip=533214;
The general form of accessing a member of structure is,
                                     object_name.member_name;
In the above example, we are initialized directly we also be able to initialized the value to a structure field at run time as follows:
                                     scanf("%lu",&sinfo.zip);
Therefore, to print the name on the screen, write
printf("%lu",sinfo.zip);
Since name is a character array, you can access the individual characters of sinfo.name by indexing name. For example, you can print the contents of the sinfo.name one character at a time by using the following code:
for(i=0;sinfo.name[i];i++)
{
printf("%c",sinfo.name[i]);
}
Structure Assignment: The information contained in one structure can be assigned to another structure of the same type using a single assignment statement.
//Example Program for Structure Assignment
#include<stdio.h>
struct
{
int a,b;
}var1,var2;
main()
{
var1.a=929;    //assigning a value to the variable a through structure variable var1
var2=x;           //assigning one structure variable to another
printf("%d\t%d",var1.a,var2.b);
}
Arrays of Structures: Structures are often arrayed. To declare an array of structures, you must first define a structure and then declare an array variable of that type. For example, to declare a 100- element array of structures of type addr defined earlier, write
struct addr sinfo[100];
To access a specific structure, index the array name. For example, to print the zip code of structure 3, write
printf("%ul",sinfo[2].zip);
Like all array variable, arrays of structures begin indexing at 0. Let us see the following code,
struct addr
{
char name[30];
char street[40];
char city[20];
char statte[10];
unsigned long int zip;
}sinfo[100];
Passing Structures to Functions: In this topic we discuss about
1) How a structure member passed to a function?
2) How a structure passed to a function?
1)  Passing Structure member to a function: When you pass a member of a structure to a function, you are passing the value of that member to the function. For example,
consider,
struct funst
{
char x;
int y;
float z;
char s[10];
}var1;
Here are some examples of each member being passed to a function:
fun1(var1.x);           //passes character value of x
fun2(var1.y);            //passes integer value of y
fun3(var1.z);           //passes float value of z
fun4(var1.s);            //passes address of string s
fun5(var1.s[2]);       //passes character value of s[2]
In each case, it is the value of a specific element that is passed to the function. It does not matter that the element is part of a larger unit.
If you wish to pass the address of an individual structure member, put the & operator before the structure name. For example, to pass the address of the members of the structure mike, write,
fun1(&var1.x);
fun2(&var1.y);
fun3(&var1.z);
fun4(var1.s);
fun5(&var1.s[2]);
Note that & operator proceeds the structure name, not the individual name. Note also that s already signifies an address, so no & required.

08:18 Share:
About Kalyan Kurasala

I am a B.Tech Student at Prasiddha College Of Engineering and Technology in Anathavaram.

0 comments:

Get updates in your email box
Complete the form below, and we'll send you the best coupons.

Deliver via FeedBurner
back to top