Skip to content

Latest commit

 

History

History
82 lines (61 loc) · 1.82 KB

Unit-14.md

File metadata and controls

82 lines (61 loc) · 1.82 KB

Structures

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Its is kind of similar to the class in other programming languages like we have a class in which  there are multiple methods same in structure we have multiple values.

Unlike an array,  a structure can contain many different data types (int, float, char, etc.).

We can define a structure like this.

struct MyStructure {   // Structure declaration
  int myNum;           // Member (int variable)
  char myLetter;       // Member (char variable)
}; // End the structure with a semicolon 

Example of a structure

// Create a structure called myStructure
struct myStructure {
  int myNum;
  char myLetter;
};

int main() {
  // Create a structure variable of myStructure called s1
  struct myStructure s1;

  // Assign values to members of s1
  s1.myNum = 13;
  s1.myLetter = 'B';

  // Print values
  printf("My number: %d\n", s1.myNum);
  printf("My letter: %c\n", s1.myLetter);

  return 0;
} 

To assign a string to a structure type. we used **strcpy()** function.

struct myStructure {
  int myNum;
  char myLetter;
  char myString[30]; // String
};

int main() {
  struct myStructure s1;

  // Assign a value to the string using the strcpy function
  strcpy(s1.myString, "Some text");

  // Print the value
  printf("My string: %s", s1.myString);

  return 0;
} 

Simpler syntax

// Create a structure
struct myStructure {
  int myNum;
  char myLetter;
  char myString[30];
};

int main() {
  // Create a structure variable and assign values to it
  struct myStructure s1 = {13, 'B', "Some text"};

  // Print values
  printf("%d %c %s", s1.myNum, s1.myLetter, s1.myString);

  return 0;
}