## What is structure in c programming?

C structures are a derived data type.

structure in C allows to pack up more than one type of data in a single variable.

structure can be defined keyword struct.

dot `(.)` operator is used to access the structure member and arrow `(->)`operator is used to access the structure member for pointer to the structure.

Consider an instance that a car has different properties simply we can compact all the properties of that car to represnet it as a single entity. An example of the car we can see that the colour is of string type and the the number of seat is int the car model is of string type the manufacturer is string type all the properties can be combined and stored in a single variable that is structure.

## How to define c structures

We can define a structure by using keyword struct then starting with flower bracket defining the data items of the structure then close the flower bracket then a struct variable followed by a semicolon `(;)` . Structure variable name is not mandatory it is optional.

Look at the example below

``````struct structure tag {

member definition;
.
.
.
}Structure_variable_name; ``````

The structure member refers to the variable.

Refer to the example program below.

``````#include<stdio.h>
int main(int argc, char const *argv[])
{
struct my_struct
{
int variable1;
int variable2;
};
struct my_struct example;
example.variable1=100;
example.variable2=20;
printf("C Structures\nVariable 1 : %d\nVariable2 : %d",example.variable1,example.variable2);
return 0;
}`````` c structures

## Accessing the members of c structures

As we already know structure in C is a compaction of one or more variable. In order to access the member we used dot operator.

In technically we can say that structure variable followed by the dot operator which is then followed by the member name. In case of pointer to structure we use the arrow operator to access it instead of dot operator

A simple example to access structure variable is shown below

``````#include<stdio.h>
int main(int argc, char const *argv[])
{
struct my_struct
{
int variable1;
int variable2;
};
struct my_struct example;
example.variable1=100;
example.variable2=20;
printf("C Structures\nVariable 1 : %d\nVariable2 : %d",example.variable1,example.variable2);
return 0;
}`````` Accessing the members of c structures

## Pointer to c structures

A pointer to a structure can be created by using star (*) operator as shown in the example. In order to access the member for structure pointer use the arrow operator.

A simple example of accessing structure member through pointer is shown below

Step 1: create a pointer variable for structure.
`struct car *pointer;`

Step 2 : Assign the address of a structure variable by using & operator or allocate memory for structure variable dynamically.
`*pointer=&car_structure;`

Step 3: access the member of the structure variable through the pointer which is pointing at that variable using the arrow operator.
`*pointer->name;`

Try out a simple example program.

``````#include<stdio.h>
int main(int argc, char const *argv[])
{
struct my_struct
{
int variable1;
int variable2;
};
struct my_struct example;
example.variable1=100;
example.variable2=20;
struct my_struct *ptr=&example;
printf("C Structures\nVariable 1 : %d\nVariable2 : %d",ptr->variable1,ptr->variable2);
return 0;
}`````` Pointer to c structures

## Function arguments as c structures

Structure variable can be passed to a function through parameters by specifying the structure name followed by a variable like passing a variable as a parameter.

Try out simple example shown below.

``````#include<stdio.h>
struct my_struct
{
int variable1;
int variable2;
};
void my_function( struct my_struct example )
{
printf("C Structures\nVariable 1 : %d\nVariable2 : %d",example.variable1,example.variable2);
}
int main(int argc, char const *argv[])
{

struct my_struct example;
example.variable1=100;
example.variable2=20;
my_function(example);
return 0;
}`````` Function arguments as c structures

## Return structure from function

You can simply return structure variable using return statement.

You have to specify the return type of the function in the function prototype.

Try out simple program below.

``````#include<stdio.h>
struct my_struct
{
int variable1;
int variable2;
};
struct my_struct my_function( )
{
struct my_struct example;
example.variable1=100;
example.variable2=20;
return example;
}

int main(int argc, char const *argv[])
{

struct my_struct example=my_function();
printf("C Structures\nVariable 1 : %d\nVariable2 : %d",example.variable1,example.variable2);

return 0;
}`````` Return structure from function

## Array of structures

sometimes it is handy to use an array of structures. Say for instance to store data about 100 students. C provides such a feature to use an array instead of creating 100 different structure variables.

### Example

``````#include<stdio.h>
int main()
{
struct student
{
char name;
int marks;
int rollnumber;
int age;
}s;

printf("enter the students detail:name,marks,rollnumber,age\n");
for(i=0;i<100;i++)
scanf("%s%d%d%d",s[i].name,&s[i].marks,&s[i].rollnumber,&s[i].age);
for(i=0;i<100;i++)
printf("%s\t%d\t%d\t%d\n",s[i].name,s[i].marks,s[i].rollnumber,s[i].age);
return 0;
}``````

## Nested structures

One structure can be nested within another structure. In the below example to nest structures dot operator is used twice.

### Example

``````#include<stdio.h>
int main()
{
struct address
{
char phone;
char city;
};
struct student
{
char name;
int age;
struct address a;
};
struct student e={"kriston",27,"8381234","newyork"};
printf("name=%s\t age=%d \t phone=%d \t city=%s\n",e.name,e.age,e.a.phone,e.city);
return 0;
}``````

## People also ask for

### What are the uses of structures?

The structures are useful in following ways
1.Hiding a file from directory
2.Checking the memory size of the computer
3.Changing the size of the cursor
4.Clearing the contents of the screen
5.Placing the cursor at an appropriate position on screen
6.Interacting with the mouse
7.Receiving a key from the keyboard
8.sending the output to printer

Summary

• What is C structure?
• How to declare C structure.
• How to access structure member.
• Pointer to C structure.
• Pass structure to a function.
• Return a structure from a function.
{"ft_lang_label":"__label__en","ft_lang_prob":0.70395654,"math_prob":0.96472025,"size":6015,"snap":"2021-21-2021-25","text_gpt3_token_len":1392,"char_repetition_ratio":0.22558643,"word_repetition_ratio":0.14712644,"special_character_ratio":0.24704905,"punctuation_ratio":0.17453626,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9841711,"pos_list":[0,1,2,3,4,5,6,7,8,9,10],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-15T16:41:08Z\",\"WARC-Record-ID\":\"<urn:uuid:4b513139-b993-41ba-980a-33b2f876be0f>\",\"Content-Length\":\"63681\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e5e465ba-454d-4d75-946f-7a7aa951a059>\",\"WARC-Concurrent-To\":\"<urn:uuid:5afc5427-afac-4fa9-ac23-3a2a6eef35ab>\",\"WARC-IP-Address\":\"45.130.228.237\",\"WARC-Target-URI\":\"https://scholarsoul.com/c-structures/\",\"WARC-Payload-Digest\":\"sha1:I76XDZPZMXC3BEJ7I2VNQDN7HRTIH5BX\",\"WARC-Block-Digest\":\"sha1:USFAHW5TS5MJ33EDXBAHLJ3KNNY7M37P\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487621450.29_warc_CC-MAIN-20210615145601-20210615175601-00306.warc.gz\"}"}