The Ultimate Guide to Using Structures in C Programming

Structures in C

A Structure is declared using the key word ‘struct’ and after that we should write the structure name, i.e store. Within the left and right curly braces, we should declare the elements that consist of the structure. Once the structure is declared as shown in below, we can declare variables of the structure as:

    struct store st;

Here st is the variable of the type structure store. A memory is not allocated to the structure when it is declare. Memory is allocated only when we declare variables to the structure, so that after declaring ‘st’, the compiler will allocate memory for the structure.It is possible to declare several structure variables st1, st2,etc.

Accessing structure elements:

We can refer to the structure Using ‘structure member operator’ or ‘dot’ operator denoted by a dot (.) that is useful to the elements through structure varible.

st.no;
st.name;
st.charge;

Declaring and Initializing the structure:

struct store
{
   int bookno;
   char bookname[20];
   float price;
}

struct store st1 ={ 1, "C programming", 150.00};
struct store st2 ={ 2, "Data structurre", 180.00};

Here, we are creating two structure variables, st1 and st2 and initializing them with data. The structure varible s1 contains 1 for bookno and “C programming” for bookname and 150.00 for price.They can be referred as st1.bookno, st1.bookname and st1.price. Similarly the structure variable st2 contains 2 for bookno, “Data structure” for bookname and 180.00 for price. They can be referred as st2.bookno, st2.bookname, and st.price.

Naveed Tawargeri
 

Hi, I'm Naveed Tawargeri, and I'm the owner and creator of this blog. I'm a Software Developer with a passion for Programming.