C Structures
\\n\\n-- Learning is not just about technology, but also about dreams!
\\n\\n- \\n
- Home \\n
- HTML \\n
- JavaScript \\n
- CSS \\n
- Vue \\n
- React \\n
- Python3 \\n
- Java \\n
- C \\n
- C++ \\n
- C# \\n
- AI \\n
- Go \\n
- SQL \\n
- Linux \\n
- VS Code \\n
- Bootstrap \\n
- Git \\n
- Local Bookmarks \\n
- \\n
- Vue3 Tutorial \\n
- Vue2 Tutorial \\n
- \\n
- Bootstrap3 \\n
- Bootstrap4 \\n
- Bootstrap5 \\n
- \\n
- Machine Learning \\n
- PyTorch \\n
- TensorFlow \\n
- Sklearn \\n
- NLP \\n
- AI Agent \\n
- Ollama \\n
- Coding Plan \\n
C Tutorial
\\n\\nC Language TutorialC IntroductionC Environment SetupC VScodeC Program StructureC Basic SyntaxC Data TypesC VariablesC ConstantsC Storage ClassesC OperatorsC Decision MakingC LoopsC FunctionsC Scope RulesC ArraysC enum (Enumeration)C PointersC Function Pointers and CallbacksC StringsC StructuresC UnionsC Bit FieldsC typedefC Input & OutputC File I/OC PreprocessorsC Header FilesC Type CastingC Error HandlingC RecursionC Variable ArgumentsC Memory ManagementC Undefined BehaviorC Command Line ArgumentsC Safe FunctionsC Sorting AlgorithmsC Project StructureC ExamplesC Classic 100 ExamplesC Quiz
\\n\\nC Standard Library
\\n\\nC Standard Library - Reference Manual<a href="#" title="C Standard Library - ">C Standard Library - <assert.h><a href="#" title="C Standard Library - ">C Standard Library - <ctype.h><a href="#" title="C Standard Library - ">C Standard Library - <errno.h><a href="#" title="C Standard Library - ">C Standard Library - <float.h><a href="#" title="C Standard Library - ">C Standard Library - <limits.h><a href="#" title="C Standard Library - ">C Standard Library - <locale.h><a href="#" title="C Standard Library -
\\n\\n\\n\\n\\n\\nC Structures
\\n\\nC arrays allow defining variables that can store items of the same type. A structure is another user-defined data type available in C programming, which allows you to store data items of different types.
\\n\\nThe data members of a structure can be of basic data types (such as int, float, char, etc.), or they can be of other structure types, pointer types, etc.
Structures are used to represent a record. Suppose you want to track the movement of books in a library, you might want to track the following attributes for each book:
\\n\\n- \\n
- Title \\n
- Author \\n
- Subject \\n
- Book ID \\n
Defining a Structure
\\n\\nA structure definition consists of the keyword struct and the structure name. The structure name can be defined as needed.
The struct statement defines a new data type with multiple members. The format of the struct statement is as follows:
struct tag {\\n member-list\\n member-list\\n member-list\\n ...\\n} variable-list ;\\n\\n\\ntag is the structure tag.
\\n\\nmember-list is a standard variable definition, such as int i; or float f;, or any other valid variable definition.
variable-list is the structure variable, defined at the end of the structure, before the last semicolon. You can specify one or more structure variables. Here is how the Book structure is declared:
\\n\\nstruct Books\\n{\\n char title;\\n char author;\\n char subject;\\n int book_id;\\n} book;\\n\\n\\nIn general, at least 2 of the 3 parts tag, member-list, variable-list must appear. Here are examples:
\\n\\n// This declaration declares a structure with 3 members, of type int a, char b, and double c.\\n// It also declares the structure variable s1.\\n// This structure does not specify a tag.\\nstruct\\n{\\n int a;\\n char b;\\n double c;\\n} s1;\\n\\n// This declaration declares a structure with 3 members, of type int a, char b, and double c.\\n// The structure tag is named SIMPLE, and no variable is declared.\\nstruct SIMPLE\\n{\\n int a;\\n char b;\\n double c;\\n};\\n\\n// Using the SIMPLE tag, we declare variables t1, t2, and t3.\\nstruct SIMPLE t1, t2, *t3;\\n\\n// You can also use typedef to create a new type.\\ntypedef struct\\n{\\n int a;\\n char b;\\n double c;\\n} Simple2;\\n\\n// Now you can use Simple2 as a type to declare new structure variables.\\nSimple2 u1, u2, *u3;\\n\\n\\nIn the above declarations, the first and second declarations are treated by the compiler as two completely different types, even though their member lists are identical. If you assign t3 = &s1;, it would be illegal.
A structure's members can include other structures, or pointers to its own structure type. Such pointers are typically used to implement more advanced data structures like linked lists and trees.
\\n\\n// This structure declaration includes another structure.\\nstruct COMPLEX\\n{\\n char string;\\n struct SIMPLE a;\\n};\\n\\n// This structure declaration includes a pointer to its own type.\\nstruct NODE\\n{\\n char string;\\n struct NODE *next_node;\\n};\\n\\n\\nIf two structures include each other, you need to perform an incomplete declaration for one of them, as shown below:
\\n\\nstruct B; // Incomplete declaration for structure B.\\n\\n// Structure A contains a pointer to structure B.\\nstruct A\\n{\\n struct B *partner;\\n // other members;\\n};\\n\\n// Structure B contains a pointer to structure A. After A is fully declared, B is also fully declared.\\nstruct B\\n{\\n struct A *partner;\\n // other members;\\n};\\n\\n\\n\\n\\n
Initializing Structure Variables
\\n\\nLike other variable types, structure variables can be assigned initial values at the time of definition.
\\n\\nExample
\\n\\n#include <stdio.h>\\n\\nstruct Books\\n{\\n char title;\\n char author;\\n char subject;\\n int book_id;\\n} book ={"C Language", "", "Programming Language", 123456};\\n\\nint main( )\\n{\\n printf( "title : %sn author: %sn subject: %sn book_id: %dn", book.title, book.author, book.subject, book.book_id);\\n}\\n\\n\\nThe output of the execution is:
\\n\\ntitle : C Language\\nauthor: \\nsubject: Programming Language\\nbook_id: 123456\\n\\n\\nAccessing Structure Members
\\n\\nTo access structure members, we use the member access operator (.). The member access operator is a period between the structure variable name and the structure member we want to access. You can use the struct keyword to define variables of a structure type. The following example demonstrates the use of structures:
Example
\\n\\n#include <stdio.h>\\n#include <string.h>\\n\\nstruct Books\\n{\\n char title;\\n char author;\\n char subject;\\n int book_id;\\n};\\n\\nint main( )\\n{\\n struct Books Book1; /* Declare Book1 of type Books */\\n struct Books Book2; /* Declare Book2 of type Books */\\n\\n /* Book1 specification */\\n strcpy( Book1.title, "C Programming");\\n strcpy( Book1.author, "Nuha Ali");\\n strcpy( Book1.subject, "C Programming Tutorial");\\n Book1.book_id = 6495407;\\n\\n /* Book2 specification */\\n strcpy( Book2.title, "Telecom Billing");\\n strcpy( Book2.author, "Zara Ali");\\n strcpy( Book2.subject, "Telecom Billing Tutorial");\\n Book2.book_id = 6495700;\\n\\n /* Print Book1 info */\\n printf( "Book 1 title : %sn", Book1.title);\\n printf( "Book 1 author : %sn", Book1.author);\\n printf( "Book 1 subject : %sn", Book1.subject);\\n printf( "Book 1 book_id : %dn", Book1.book_id);\\n\\n /* Print Book2 info */\\n printf( "Book 2 title : %sn", Book2.title);\\n printf( "Book 2 author : %sn", Book2.author);\\n printf( "Book 2 subject : %sn", Book2.subject);\\n printf( "Book 2 book_id : %dn", Book2.book_id);\\n\\n return 0;\\n}\\n\\n\\nWhen the above code is compiled and executed, it produces the following result:
\\n\\nBook 1 title : C Programming\\nBook 1 author : Nuha Ali\\nBook 1 subject : C Programming Tutorial\\nBook 1 book_id : 6495407\\nBook 2 title : Telecom Billing\\nBook 2 author : Zara Ali\\nBook 2 subject : Telecom Billing Tutorial\\nBook 2 book_id : 6495700\\n\\n\\nStructures as Function Arguments
\\n\\nYou can pass structures as function arguments, similar to other variable types or pointers. You can access structure variables in the same way as shown in the example above:
\\n\\nExample
\\n\\n#include <stdio.h>\\n#include <string.h>\\n\\nstruct Books\\n{\\n char title;\\n char author;\\n char subject;\\n int book_id;\\n};\\n\\n/* Function declaration */\\nvoid printBook( struct Books book );\\n\\nint main( )\\n{\\n struct Books Book1; /* Declare Book1 of type Books */\\n struct Books Book2; /* Declare Book2 of type Books */\\n\\n /* Book1 specification */\\n strcpy( Book1.title, "C Programming");\\n strcpy( Book1.author, "Nuha Ali");\\n strcpy( Book1.subject, "C Programming Tutorial");\\n Book1.book_id = 6495407;\\n\\n /* Book2 specification */\\n strcpy( Book2.title, "Telecom Billing");\\n strcpy( Book2.author, "Zara Ali");\\n strcpy( Book2.subject, "Telecom Billing Tutorial");\\n Book2.book_id = 6495700;\\n\\n /* Print Book1 info */\\n printBook( Book1 );\\n\\n /* Print Book2 info */\\n printBook( Book2 );\\n\\n return 0;\\n}\\n\\nvoid printBook( struct Books book )\\n{\\n printf( "Book title : %sn", book.title);\\n printf( "Book author : %sn", book.author);\\n printf( "Book subject : %sn", book.subject);\\n printf( "Book book_id : %dn", book.book_id);\\n}\\n\\n\\nWhen the above code is compiled and executed, it produces the following result:
\\n\\nBook title : C Programming\\nBook author : Nuha Ali\\nBook subject : C Programming Tutorial\\nBook book_id : 6495407\\nBook title : Telecom Billing\\nBook author : Zara Ali\\nBook subject : Telecom Billing Tutorial\\nBook book_id : 6495700\\n
YouTip