Showing posts with label Structures in C. Show all posts
Showing posts with label Structures in C. Show all posts

Wednesday, 15 April 2020

Nested structures in C


When a structure contains another structure as its member, it is called a nested structure.
Syntax:
  struct innerStruct{
     -------------------
     -------------------
};

struct outerStruct{
     -------------------
     -------------------
   struct innerStruct inner;
};

For example, we want to store information about an employee-empName, empNo, dept,address. Address can be broken into houseNo,city,pincode so we will store address details in another structure named Address and other information about employee in Employee structure.
#include<stdio.h>
//Inner structure
struct Address{
  char houseNo[20];
  char city[20];
};

//Outer structure
struct Employee{
  char empName[25];
  char  dept[15];
  struct Address add;
};

int main(){
  struct Employee emp;
  printf("Enter Employee Name: ");
  scanf("%s",&emp.empName);
  printf("Enter Employee Department: ");
  scanf("%s",&emp.dept);

  printf("Enter Employee Address details:\n ");
  printf("Enter Employee House No: ");
  scanf("%s",&emp.add.houseNo);
  printf("Enter Employee City: ");
  scanf("%s",&emp.add.city);

  printf("\nEmployee Details\n");
  printf("Name :%s\n",&emp.empName);
  printf("Department :%s\n",&emp.dept);
  printf("House No :%s\n",&emp.add.houseNo);
  printf("City :%s\n",&emp.add.city);
  return 0;
}

Output:


Note that inner structure must be declared before outer structure.

You may also like:

 Structure in C
 Typedef in C with examples
 Array of Structures in C with Example
 Self Referential Structures in C
 Passing structure to a function in C with example

Please comment and share if you find it useful.


Monday, 6 April 2020

Self Referential Structures in C with example

A Self Referencial Structure is a structure which have one or more pointer variables of that structure type as their member(s).

Consider following example:
struct student{
   int rollno;
   float marks;
};

Above structure declaration is standard declaration we studied earlier. But if we add one or more pointer variables of structure student type as the member(s) of student structure, then it will become self referencial structure. See the example below:

struct student{
     int rollno;
     float marks;
     struct student *next;

}

In the above example ‘next’ is a pointer to a structure of type ‘student. Hence, the structure ‘student is a self-referential structure with ‘next as the referencing pointer.




A self referential structure is used to create data structures like linked lists, stacks, queues etc.


You may also like:
 Structure in C
 Typedef in C with examples
 Array of Structures in C with Example

Please comment and share if you find it useful.

  

Saturday, 9 March 2019

Structures in C (Part 1)

As you are aware that arrays store elements of similar type. Contrary to this, structures are objects that contain more than one item.
Definition:
  • A structure contains a number of data types grouped together.
  • These data types may or may not be of the same type.
In this article, you will learn following fundamental aspects of structures:
(a) declaration of a structure
(b) accessing of structure elements
(c) Initializing values to structure element(s)
(d) how structure elements are stored in memory

(a) declaration of a structure
The general form of a structure declaration statement is given below:
struct structure_name{
       datatype structure_element 1 ;
       datatype structure_element 2 ;
       datatype structure_element 3 ;
        ......
        ......

   } structure_variable(s);

Points to remember about above declaration:
  • The first line contains the struct keyword, then the optional structure_name:struct structure_name {
  • The structure_name can be used to create a copy of the structure. An opening brace follows the structure_name (or the struct keyword, if the structure_name is not used). This brace signals to the compiler that the next lines are member definitions. Each member definition consists of a variable type and a name. The members can be any valid variable type, including arrays, structures, and unions.
  • Following the last member name is a closing brace and the optional structure_variable, as follows:} structure_ variable(s) ;
  • The closing brace in the structure type declaration must be followed by a semicolon.
  • A structure type declaration does not reserve any space in memory. All a structure declaration does is, it defines the ‘form’ of the structure.

When using the structure_ variable and the structure_name, you can choose any of the following:
  • If a structure_ variable is not specified and a structure _name is specified, the structure is being defined but not declared.
  • If a structure_ variable is specified and a structure _name is not specified, the structure is being declared but not defined.
  • If a structure_ variable and a structure _name are provided, the structure is being both defined and declared.
  • If neither a structure_ variable nor a structure _name is provided, a compile-time error will result. 

If you want to initialize the structure, you must have a structure_ variable because
it signals the compiler that this is a declaration. The structure_ variable is also necessary if you want to refer to the structure.
After the structure_variable are optional initializers:
{initializer_values};
Example:
struct student
{
  int rollno;
  int marks;
} stu1;

(b).Accessing Structure Elements
In arrays we can access individual elements of an array using a subscript. Structures use a different scheme. They use a dot (.) operator.
Note that before the dot there must always be a structure variable and after the dot there must always be a structure element.
Syntax:
   structure_variable.structure_element;

If the structure_element is also a structure ,the structure_element name is followed by a period and its structure_element name:
structure_variable.memberstructure. structure_element

 Example:
   stu1.rollno;

In this example, I will show you how to declare a structure, assign values and access the elements of structure.
#include<stdio.h>
struct student
{
  int rollno;
  int marks;
} stu1,stu2;
int main(){

  return 0;
}


(c).Initializing values to structure element(s)
Unlike standard variables, the syntax for initialising structure variables is different.The structure elements are accessed using the dot notation.
One can either initialize a structure by initializing the individual elements as shown below:
      stu1. rollno = 6532;
      stu1. marks = 89;

 or by simply listing the element's value inside curly braces, with each value separated by a comma.
     stu1 = {6532,89};

A structure variable can be assigned  to another providing that the structures have the same number of members and that the members are of the same data type.
  Example : stu2=stu1;

(d).How Structure Elements are Stored

Whatever be the elements of a structure, they are always stored in contiguous memory locations.

Example:
#include <stdio.h>

struct student{
    char *name;
    int rollno;
    int marks;
}stu1;
int main()
{
     /*Assigning the values of each struct element here*/
     stu1.name = "Steve";
     stu1.rollno = 1234;
     stu1.marks = 30;

     /* Displaying the values of struct element */
     printf("Student Name is: %s", stu1.name);
     printf("\nStudent roll no is: %d", stu1.rollno);
     printf("\nStudent marks is: %d", stu1.marks);
     return 0;

}

Please comment if you find anything incorrect, or you want to improve the topic discussed above.