C – Structures

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/c-structures/
PINTEREST
PINTEREST
INSTAGRAM

C Programming Tutorial Index

  1. Introduction To Programming
  2. C Setup Development Environment
  3. C Hello World and Compile First Application
  4. C Syntax and Basics
  5. C Variables
  6. C Numeric Variable Types (Integer, Long, Float)
  7. C String Variables
  8. C Constants and Define Processor
  9. C Operators
  10. C If Else and Switch Case Conditionals
  11. C While Loops
  12. C For Loops
  13. C Arrays
  14. C Functions
  15. C Structure
  16. C Union
  17. C File Operations (Open, Read, Write)
  18. C Pointers
  19. C Type Casting
  20. C Header Files and Include

C provides primitive data types like int , char , float etc. But in order to solve real world problems we need more than these types. Newer object oriented languages have a lot of features to implement real world situations. For example C++ provides Object Oriented features where we can implement real world objects. C lacks of these features but provides struct type. Struct is used to provide composite data type which can provides multiple primitive types.

Defining Structure

Defining a struct is similar to union. We will provide the elements we want to store in a struct and the name of the struct like following syntax.

Syntax can provide some hint about struct but the best way to understand and learn is defining struct as a real world example. In this example we will create a struct named Student which have following members.

  • name holds student name as char variables
  • id holds student id as int

We have used only two member to make things simple but there is not limit about the members other than memory.

Initialize

We can initialize new structs variables like below just providing the struct keyword with the struct name and the variable name we want to use. Here we create a struct named s1 with Student struct type.

LEARN MORE  Linux Bash Define, Call Functions

Accessing Structure Members

We have defined struct members id and name . We need to set and get these members values.We can simply access them with the struct variables name and the member name.

We have set the id with the following line

We can also access same syntax to the id variable like below.

Structure As Function Arguments

We have seen that structures provide good flexibility. We generally use structures to pass values to the functions. In this part we will look how can we pass structure variable to the function. We need to define struct parameter as function argument like defining normal struct.

C Programming Tutorial Index

  1. Introduction To Programming
  2. C Setup Development Environment
  3. C Hello World and Compile First Application
  4. C Syntax and Basics
  5. C Variables
  6. C Numeric Variable Types (Integer, Long, Float)
  7. C String Variables
  8. C Constants and Define Processor
  9. C Operators
  10. C If Else and Switch Case Conditionals
  11. C While Loops
  12. C For Loops
  13. C Arrays
  14. C Functions
  15. C Structure
  16. C Union
  17. C File Operations (Open, Read, Write)
  18. C Pointers
  19. C Type Casting
  20. C Header Files and Include
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/c-structures/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)