# C struct

## How to define structures?

Before you can create structure variables, you need to define its data type. To define a struct, the `struct` keyword is used.

### Syntax of struct

```struct structureName
{
dataType member1;
dataType member2;
...
};
```

Here is an example:

```struct Person
{
char name[50];
int citNo;
float salary;
};
```

Here, a derived type `struct Person` is defined. Now, you can create variables of this type.

## Create struct variables

When a struct type is declared, no storage or memory is allocated. To allocate memory of a given structure type and work with it, we need to create variables.

Here’s how we create structure variables:

```struct Person
{
char name[50];
int citNo;
float salary;
};

int main()
{
struct Person person1, person2, p[20];
return 0;
}```

Another way of creating a struct variable is:

```struct Person
{
char name[50];
int citNo;
float salary;
} person1, person2, p[20];
```

In both cases, two variables person1person2, and an array variable p having 20 elements of type `struct Person` are created.

## Access members of a structure

There are two types of operators used for accessing members of a structure.

1. `.` – Member operator
2. `->` – Structure pointer operator (will be discussed in the next tutorial)

Suppose, you want to access the salary of person2. Here’s how you can do it.

`person2.salary`

``````// Program to add two distances (feet-inch)
#include <stdio.h>
struct Distance
{
int feet;
float inch;
} dist1, dist2, sum;

int main(){
printf("1st distancen");
printf("Enter feet: ");
scanf("%d", &dist1.feet);

printf("Enter inch: ");
scanf("%f", &dist1.inch);
printf("2nd distancen");

printf("Enter feet: ");
scanf("%d", &dist2.feet);

printf("Enter inch: ");
scanf("%f", &dist2.inch);

sum.feet = dist1.feet + dist2.feet;
sum.inch = dist1.inch + dist2.inch;

// changing to feet if inch is greater than 12
while (sum.inch >= 12)
{
++sum.feet;
sum.inch = sum.inch - 12;
}

printf("Sum of distances = %d'-%.1f"", sum.feet, sum.inch);
return 0;
}``````

Output

```1st distance
Enter feet: 12
Enter inch: 7.9
2nd distance
Enter feet: 2
Enter inch: 9.8
Sum of distances = 15'-5.7"```

### Keyword typedef

We use the `typedef` keyword to create an alias name for data types. It is commonly used with structures to simplify the syntax of declaring variables.

This code

```struct Distance{
int feet;
float inch;
};

int main() {
struct Distance d1, d2;
}
```

is equivalent to

```typedef struct Distance{
int feet;
float inch;
} distances;

int main() {
distances d1, d2;
}
```

## Nested Structures

You can create structures within a structure in C programming. For example,

```struct complex
{
int imag;
float real;
};

struct number
{
struct complex comp;
int integers;
} num1, num2;
```

Suppose, you want to set imag of num2 variable to 11. Here’s how you can do it:

```num2.comp.imag = 11;
```

## Why structs in C?

Suppose, you want to store information about a person: his/her name, citizenship number, and salary. You can create different variables namecitNo and salary to store this information.

What if you need to store information of more than one person? Now, you need to create different variables for each information per person: name1citNo1salary1name2citNo2salary2,etc.

A better approach would be to have a collection of all related information under a single name `Person` structure and use it for every person.

# Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

## Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

# Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

`Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.`

# Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!

https://setscholars.net/c-programming-tutorials-for-beginners-c-structs-and-pointers/

C programming tutorials for Beginners – C Structure and Function

Algorithm in C – LinkedList Data Structure