Introduction
Structures group related data together. Accessing structure members lets you use or change each piece of data inside the group.
struct StructName {
dataType member1;
dataType member2;
// more members
};
struct StructName variableName;
// Access members using dot operator
variableName.member1 = value;
value = variableName.member2;struct Person {
char name[50];
int age;
};
struct Person p1;
p1.age = 25;struct Book {
char title[100];
int pages;
};
struct Book b1;
strcpy(b1.title, "C Programming");
b1.pages = 300;#include <stdio.h>
#include <string.h>
struct Student {
char name[30];
int grade;
};
int main() {
struct Student s1;
strcpy(s1.name, "Alice");
s1.grade = 90;
printf("Student Name: %s\n", s1.name);
printf("Student Grade: %d\n", s1.grade);
return 0;
}struct Point { int x; int y; } p;, which is the correct way to assign 10 to member x?p is a structure variable, not a pointer.p.x = 10; is correct.struct Person {
char name[10];
int age;
};
int main() {
struct Person p = {"Amy", 25};
printf("%s %d", p.name, p.age);
return 0;
}p is initialized with "Amy" for name and 25 for age.printf prints p.name as a string and p.age as an integer, so output is Amy 25.struct Data {
int id;
float value;
};
int main() {
struct Data d;
d->id = 5;
d.value = 3.14;
return 0;
}d is a structure variable, not a pointer, so d->id is incorrect.d.id = 5; is correct.struct Employee {
char name[20];
int id;
};
int main() {
struct Employee emp1 = {"John", 101};
struct Employee *ptr = &emp1;
ptr->id = 202;
printf("%s %d", emp1.name, emp1.id);
return 0;
}
What will be the output?ptr points to emp1, so ptr->id changes emp1.id.ptr->id = 202;, emp1.id becomes 202. Printing emp1.name and emp1.id outputs John 202.Pass the test and this pattern moves unknown