Basic Structures in C


Structures are one of the data type available in C language. As you know, this is also an user defined data type. It is like to hold multiple number of variables as a bundle like an array but the only difference between arrays and structures is holding different kind of variables but not the same type.

C basic tutorials

C basic tutorials

Structures are used when you want to process data of multiple data types but you still want to refer to the data as a single entity. Structures are similar to records in Cobal or Pascal. For example, you might want to process information on students in the categories of name and marks (grade percentages). Here you can declare the structure ‘student’ with the fields ‘name’ and ‘marks’, and you can assign them appropriate data types. These fields are called members of the structure. A member of the structure is referred to in the form of structurename.membername.

 struct simple_structure

{         
            int i;
            char c;
};

int main()

{
            struct simple_structure v;

            v.i=10;

            v.c='d';

            printf(“i value = %d”, v.i);

            printf(“c value = %c”,v.c);

            return;

}

The very Basic points to Remember :

  • struct is the key word we use in C for using structures.
  • Before we use any strusture variable for we first define that perticular structure.
  • What ever we declare inside structure block those will become that structure elements.
  • To use the items or structure elements which we declared inside the structure block, we need to use that structure name followed by the ‘.‘ or ‘‘ operator.

When to use ‘.’ and when to use ‘→’ :

Just consider this example .

   struct  simple_structure v1;

   struct simple_structure *v2;

  we declared two structure variables one is normal and second is pointer to structure. In case of normal structure variable we need to use ‘.’ and in case of pointer variable we need to use → .

             v1.i      normal structure variable case

v2->i    pointer structure variable case

 I am going to take one more program to explain further more in detail. The below section may help to clear your doubts what you got by reading in above paras.

 Program

struct student_info                 \\ A

{

    char name[30];            \\ B

    float marks;              \\ C

}   student1, student2;      \\ D

main ( )

{

    struct student_info student3; \\ E

    char s1[30];             \\ F

    float f;                 \\ G

    scanf ("%s", name);      \\ H

    scanf (" %f", & f);       \\ I

    student1.name = s1;      \\ J

    student2.marks = f;      \\ K

    printf (" Name is %s \n", student1.name);      \\ L

    printf (" Marks are %f \n", student2.marks);   \\ M

}

Explanation

  • Statement A defines the structure type student_info. It has two members: name and marks. We call them structure members or structure elements.
  • Statement B defines the structure member name of the type char and it stores 30 characters.
  • Statement C defines the structure member marks of the type float.
  • Statement D defines two structure variables: structure1 and structure2. In the program you have to use variables only. Thus struct student is the data type, just as normal data type like int and student1 is the variable.
  • You can define another variable, student3, by using the notations as  I specified in statement E.
  • You can define two local variables by using statements F and G.
  • Statement J assigns s1 to a member of the structure. The structure member is referred to as structure variablename.membername. The member student1.name is just like an ordinary string, so all the operations on the string are allowed. Similarly, statement J assigns a value to student1.marks
  • Statement L prints the marks of student1 just as an ordinary string.

 One sincere request to all the readers, please comment down and correct us, this site is trying to help people to understand embedded technology, this is only possible with your comments and corrections. Please subscribe to the newsletter to get latest project/article updates right in your email inbox. Take few moments and like zembedded facebook page, follow on tweeter, thanks!

 

Related posts:

About author

This article was written by admin

Admin has over twenty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles. Working presently as Development Manager in India. A firm Believer in Knowledge grows when it shared.

Comments

Comments (2)
    • jalaja says - Posted: March 23, 2013

      this tutorial is very usefull thanks…keep it up zembedded!

Leave your comment

Your email address will not be published. Required fields are marked *