Sunday, 12 June 2011

what is Structure in C? Different between Array and structure?Initializing Structures in C programming

Structures In C


A structure is a collection of one or more variables, possibly of different data types, grouped together under a single name for convenient handling. Is short structure in C programming can be defined as

  • A structure in C is a collection of items of different types. You can think of a structure as a "record" is in Pascal or a class in Java without methods.
  • Structures, or structs, are very useful in creating data structures larger and more complex than the ones we have discussed so far.
  • Simply you can group various built-in data types into a structure.
  • Object conepts was derived from Structure concept. You can achieve few object oriented goals using C structure but it is very complex.

Following is the example how to define a structure.

struct student {

char firstName[20];

char lastName[20];

char SSN[9];

float gpa;

};

Now you have a new datatype called student and you can use this datatype define your variables of student type:

struct student student_a, student_b;

or an array of students as

struct student students[50];

Another way to declare the same thing is:

struct {

char firstName[20];

char lastName[20];

char SSN[10];

float gpa;

} student_a, student_b;

All the variables inside an structure will be accessed using these values as student_a.firstNamewill give value of firstName variable. Similarly we can aqccess other variables.

Different between Array and structure

Array

Structure

i. Data Collection

Array is a collection of homogeneous data.

Structure is a collection of heterogeneous data.

ii. Element Reference

Array elements are referred by subscript.

Structure elements are referred by its unique name.

iii. Access Method

Array elements are accessed by it's position or subscript.

Stucture elements are accessed by its object as '.' operator.

iv. Data type

Array is a derived data type.

Structure is user defined data type.

v. Syntax

array_name[size];

struct struct_name
{
    structure element 1;
    structure element 2;
        ----------
        ----------
        structure element n;
}struct_var_nm;

vi. Example

int rno[5];

struct item_mst
{
    int rno;
    char nm[50];
}it;

Initializing Structures

An initializer for a structure is a brace-enclosed comma-separated list of values. An initializer is preceded by an equal sign (=). In the absence of designations, memory for structure members is allocated in the order declared, and memory address are assigned in increasing order, with the first component starting at the beginning address of the structure name itself. You do not have to initialize all members of a structure. The default initializer for a structure with static storage is the recursive default for each component; a structure with automatic storage has none.

The following subsection pertains to C only.

Named members of a structure can be initialized in any order; any named member of a union can be initialized, even if it is not the first member. A designator identifies the structure or union member to be initialized. The designator for a structure or union member consists of a dot and its identifier (.fieldname). A designator list is a combination of one or more designators for any of the aggregate types. A designation is a designator list followed by an equal sign (=).

A designator identifies a first subobject of the current object, which at the beginning of the initialization is the structure itself. After initializing the first subobject, the next subobject becomes the current object, and its first subobject is initialized; that is, initialization proceeds in forward order, and any previous subobject initializations are overridden.

The initializer for an automatic variable of a structure or any aggregate type can be a constant or non-constant expression. Allowing an initializer to be a constant or non-constant expression is ANSI C language feature.

The following declaration of a structure is a definition that contains designators, which remove some of the ambiguity about which subobject will be initialized by providing an explicit initialization. The following declaration defines an array with two element structures. In the excerpt below,[0].a and [1].a[0] are designator lists.

struct { int a[5], b; } game[] =

{ [0].a = { 1 }, [1].a[0] = 2 };

/* game[0].a[0] is 1, game[1].a[0] is 2, and all other elements are zero. */

The declaration syntax uses braces to indicate initializer lists, yet is referred to as a bracketed form. A fully bracketed form of a declaration is less likely to be misunderstood than a terser form. The following definition accomplishes the same thing, is legal and shorter, but inconsistently bracketed, and could be misleading. Neither b structure member of the two struct game objects is initialized to 2.

struct { int a[5], b; } game[] =

{ { 1 }, 2 };

/* game[0].a[0] is 1, game[1].a[0] is 2, and all other elements are zero. */

Unnamed structure or union members do not participate in initialization and have indeterminate value after initialization.

Example

The following definition shows a completely initialized structure:

struct address {

int street_no;

char *street_name;

char *city;

char *prov;

char *postal_code;

};

static struct address perm_address =

{ 3, "Savona Dr.", "Dundas", "Ontario", "L4B 2A1"};

No comments:

Post a Comment