Programming and Problem Solving

Structures - Declaration and Initialization of Structures

Declaration and initialization of structures

General form of structure

declaration Structure tagname $$left { right.$$ Datatype member1; Datatype member2; Datatype membern; $$left. right };$$ Here, struct     -   kewords Tagname  -   specifies name of structure Member1, member2 -  -  specifies the data items that make up structure. Example : Struct book $$left { right.$$ int pages; char author [30]; float price; $$left. right };$$

Structure variables

There are 3 ways of declaring structure variables

  1. struct book

$$left { right.$$ int pages; char author[30]; float price; $$left. right }b;$$ 2. struct $$left { right.$$ int pages; char author[30]; float price; Note : Tagname can be ignored if the variable is declared of the time of defining structure $$left. right }b;$$ 3. struct book $$left { right.$$ int pages; char author[30]; float price; $$left. right };$$

Initialization and accessing of structures

  • The link between a member and a structure variable is established using member operator (or) dot operator
  • Initialization can be done in the following ways
  1. struct book

$$left { right.$$ int pages; char author[30]; float price; $$left. right }b$$ =$$left { right.$$100, “balu”, 325.75$$left. right }$$; 2. struct book $$left { right.$$ int pages; char author[30]; float price; $$left. right }$$ struct b =$$left { right.$$100, “balu”, 325.75$$left. right }$$; 3. using member operator $$left { right.$$ int pages; char author[30]; float price; $$left. right };$$ Struct book b; b. pages = 100 strcpy (b.author, “balu”); b.price = 325.75; 4 . using scanf ( ) struct book $$left { right.$$ int pages; char author[30]; float price; $$left. right };$$ struct book b; scanf (“$$%d”, &b$$.pages); scanf (“$$%s$$”, b.author); scanf (“$$%f”, &b$$. price); main ( ) $$left { right.$$ struct book b clrscr ( ); printf ( “enter no of pages, author, price of book”); scanf (“$$%d%s%f, &b$$.pages, b.author, $$&b$$.price); printf(“ Details of book are”); printf(“pages =$$%d$$, author =$$ %s$$, price = $$%f$$, b.pages, b.author, b.price); getch(); $$left. right }$$

Operations on Structures

There is a relatively small number of operations which C directly supports on structures. As we've seen, we can define structures, declare variables of structure type, and select the members of structures. We can also assign entire structures:

the expression c1 = c2

would assign all of c2 to c1 (both the real and imaginary parts, assuming the preceding declarations). We can also pass structures as arguments to functions, and declare and define functions which return structures. But to do anything else, we typically have to write our own code (often as functions).

For example, we could write a function to add two complex numbers: struct complex cpx_add(struct complex c1, struct complex c2) { struct complex sum; sum.real = c1.real + c2.real; sum.imag = c1.imag + c2.imag; return sum; } We could then say things like c1 = cpx_add(c2, c3) (Two footnotes: Typically, you do need a temporary variable like sum in a function like cpx_add above that returns a structure.) One more thing you can do with a structure is initialize a structure variable while declaring it. As for array initializations, the initializer consists of a comma-separated list of values enclosed in braces {}: struct complex c1 = {1, 2}; struct complex c2 = {3, 4}; Of course, the type of each initializer in the list must be compatible with the type of the corresponding structure member.  

Uses of structures in C:

  1. C Structures can be used to store huge data. Structures act as a database.
  2. C Structures can be used to send data to the printer.
  3. C Structures can interact with keyboard and mouse to store the data.
  4. C Structures can be used in drawing and floppy formatting.
  5. C Structures can be used to clear output screen contents.
  6. C Structures can be used to check computer’s memory size etc.