How to Use Structures ("struct") in C Programming
What Is C Structure?
We use a variable in a C program to store a value, but one variable can store only a single piece of information (an integer can hold only one integer value). To store similar types of values without having to declare many variables, we use an array, which can hold numbers of similar data types. But arrays also have limitations. In a real-world application, we may deal with a set of dissimilar data types, and a single array cannot store dissimilar data.
Take a product, for example. A product can have different information to store, such as product code (an integer), product name (a char array), product price (a float), etc. To store 20 products' information, we can declare an integer array to store product code, a 2D character array to store product name, and a float array to store product price. This approach definitely achieves your goals, but what if you want to add more than 20 products? What if you want to add more information on products, such as stock, discount, tax, etc.? It will become difficult to differentiate these variables from other variables declared for calculation, etc.
To solve this problem, C language has a unique data type called structure. C structure is simply a collection of different related data types. When we use C structure, we combine different related data types in one group so that we can use and manage those variables easily.
In short, a structure holding information about a product will contain variables and arrays related to the product.
Syntax for C Structure Declaration
struct structure_name
{
data type member1;
data type member2;
…
…
};
Example:
struct products
{
char name[20];
int stock;
float price;
};
So structure declaration begins with struct keyword and with a space we need to provide a structure name. Within open and closed curly braces we can declare required and related variables, you can see it in our example structure declaration. And the most important point to remember (in the case of C structure) is that it ends with semicolon (;).
Let’s have a complete example of structure in C language.
Example of C Structure
Code Explanation
So line no.4-9 declares a C structure named “product”, this structure contains four variables to store different information about the product. In the beginning, there is a character array (char name[30]) that stores the name of the product, next we have an integer variable (int stock) to store the stock of the product and the last two variables are float type (float price, discount) to product price & discount on product respectively.
Guys we just declared the product structure and now we have to use it in main(). Line no. 14 declares a product type variable p1. Here product type variable means, in our C program product is a structure and to use that structure we need to create its variable. Declaring a product structure variable is simple just use the following syntax:
struct structure_name variable_name;
Remember struct is a C keyword, “structure_name” is the name of structure you used while declaring a C structure (in the above C program its product), and “variable_name” could be any name of your choice (in the above C program it's p1) but standard naming convention applies.
Along with declaring C structure variable p1 we have also initialized it and to initialize C structure you need to assign values in proper order. Proper order means assigning values in the order they are declared in structure. For example, in our product structure we declare variables in the following orders:
char name[30];
int stock;
float price, discount;
So for this structure proper order will be:
char name[30];
int stock;
float price;
float discount;
You don’t need to rewrite your structure, you just need to keep it in mind that structure variable initialization should be performed in an orderly manner (top – bottom and left – right manner) otherwise it will show error or you may get strange output.
So in the above program we have initialized p1 variable in the following way:
struct product p1 ={"Apple iPod Touch 32GB", 35, 298.56, 2.32}; means
struct product p1 ={char name[30], int stock, float price, float discount}; //this line of code is only assumption.
Next in line no. 16 we just printed the values stores in the product structure. You cannot print values stored in the product structure member variable (like name, stock etc.) directly, you have to link the member variable with structure variable and you can do this using (.) dot operator. For example, the character array name is unknown to main() because it is declared in structure product, so to access the member variable we will follow the following syntax:
structure_variable.member_variable;
Example:
p1.name;
We can rewrite printf("Name=%s,\nStock=%d,\nPrice=$%.2f,\nDiscount=%.2f%.", p1.name, p1.stock, p1.price,p1.discount); in the following manner:
printf(“Name = %s”,p1.name);
printf(“Stock = %d”,p1.stock);
printf(“Price = $%.2f”,p1.price);
printf(“Stock = %.2f”,p1.discount);
Here is a full working code sample of C Structure.
I hope this helps you understand C structure. If you have any questions regarding this topic, drop a comment.
© 2010 RAJKISHOR SAHU
Comments
Naveen on November 11, 2018:
What are the advantages of structures in C
Bindeshwar Singh Kushwaha on March 27, 2018:
Very useful post thanks for writing.
Sofie Norberts on November 16, 2012:
great Structure tutorials
RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on July 16, 2012:
hi rachel : please visit https://discover.hubpages.com/technology/Your-Firs...
rachel domingo on July 14, 2012:
how can i create a program .. f i am a begginer?can u help me to develop my knowledge in programming/
shab on June 21, 2012:
great
manzoor samejo on March 28, 2012:
arrays
sukhraj johal(moga) on March 16, 2012:
thnks for this tutorial...................sir.thnks 1 again....................
Java Programs from India on February 27, 2012:
Wow ... very much simple and readable ... i have bookmarked it for the future reference also. Great job ... keep the good work up ....
minahilpari26@gmail.com on February 08, 2012:
fit
suprereb defination of c language
kaifi on January 27, 2012:
great
suvarna on January 10, 2012:
i want variable explanation
MANINDER SINGH on September 20, 2011:
NIC 1
akhil on September 20, 2011:
its really great nd gud to have dis helped me a lot......
HP C4092A Cartridge on April 03, 2011:
Nice tutorials, i bookmarked it for future references. I will also recommend this to my friends.
sree on February 17, 2011:
greate ....i never seen before like this simple tutorial....
njumudeen on January 03, 2011:
Thnks for this tutorial,visit
sambaran08 from India on October 26, 2010:
Thnks for this tutorial,visit
https://hubpages.com/technology/Cnet
and follow me
sonia on September 15, 2010:
nice tutorial.thnx for such tutorial.
Pawan kumar on August 26, 2010:
i am very like you
biggig from India on June 09, 2010:
that was just great.. but still..i would love if you recommend a book for me... i have been reading let us c but seems it still not a able to understand it
RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on June 02, 2010:
yes a structure can have more than one structure variable just like we create variable of int, char, float etc.
More advance topic on structure is here: https://hubpages.com/technology/Arrays-of-Structur...
madhura on June 02, 2010:
can a structure have more than one structure variable