Professional Documents
Culture Documents
Accessing Structure Members: For Example
Accessing Structure Members: For Example
Structure members can be accessed and assigned values in a number of ways. Structure
members have no meaning individually without the structure. In order to assign a value to any
structure member, the member name must be linked with the structure variable using a
dot . operator also called period or member access operator.
For example:
#include<stdio.h>
#include<string.h>
struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
};
int main()
{
struct Student s1;
/*
s1 is a variable of Student type and
age is a member of Student
*/
s1.age = 18;
/*
using string function to add name
*/
strcpy(s1.name, "Viraaj");
/*
displaying the stored values
*/
printf("Name of Student 1: %s\n", s1.name);
printf("Age of Student 1: %d\n", s1.age);
return 0;
}
Age of Student 1: 18
Structure Initialization
Like a variable of any other datatype, structure variable can also be initialized at compile time.
struct Patient
{
float height;
int weight;
int age;
};
Array of Structure
We can also declare an array of structure variables. in which each element of the array will
represent a structure variable. Example : struct employee emp[5];
The below program defines an array emp of size 5. Each element of the array emp is of
type Employee.
#include<stdio.h>
struct Employee
{
char ename[10];
int sal;
};
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student record);
int main()
{
struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(record);
return 0;
}
void func(struct student record)
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}
6 #include <stdio.h>
7 #include <string.h>
8
9 struct student
10 {
11 int id;
12 char name[20];
13 float percentage;
14 };
15
16 void func(struct student *record);
17
18 int main()
19 {
20 struct student record;
21
22 record.id=1;
23 strcpy(record.name, "Raju");
24 record.percentage = 86.5;
25
26 func(&record);
27 return 0;
28 }
29
30 void func(struct student *record)
{
printf(" Id is: %d \n", record->id);
printf(" Name is: %s \n", record->name);
printf(" Percentage is: %f \n", record->percentage);
}
#include <stdio.h>
#include <string.h>
5
struct student
6
{
7
int id;
8
char name[20];
9
float percentage;
10
};
11
struct student record; // Global declaration of structure
12
13
void structure_demo();
14
15
int main()
16
{
17
record.id=1;
18
strcpy(record.name, "Raju");
19
record.percentage = 86.5;
20
21
structure_demo();
22
return 0;
23
}
24
25
void structure_demo()
26
{
27
printf(" Id is: %d \n", record.id);
28
printf(" Name is: %s \n", record.name);
29
printf(" Percentage is: %f \n", record.percentage);
}
1 #include <stdio.h>
2 #include <string.h>
3
4 struct student
5 {
6 int id;
7 char name[30];
8 float percentage;
9 };
10
11 int main()
12 {
13 int i;
14 struct student record1 = {1, "Raju", 90.5};
15 struct student *ptr;
16
17 ptr = &record1;
18
19 printf("Records of STUDENT1: \n");
20 printf(" Id is: %d \n", ptr->id);
21 printf(" Name is: %s \n", ptr->name);
22 printf(" Percentage is: %f \n\n", ptr->percentage);
23
24 return 0;
25 }