You need structures!
I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class.
At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out.
In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.
In C, structures are used to group different data types. They allow you to define your own data types by grouping variables of different types under a single name.
Here's a sample code that demonstrates how to use structures to store information about students in an array of structures, accept information, query by ID, and display information about that student:
#include <stdio.h>
// Define the structure for student
struct Student {
int id;
char name[50];
int age;
};
int main() {
// Declare an array of structures to store student information
struct Student students[5];
// Accept information for each student
for (int i = 0; i < 5; i++) {
printf("Enter ID for student %d: ", i + 1);
scanf("%d", &students[i].id);
printf("Enter name for student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Enter age for student %d: ", i + 1);
scanf("%d", &students[i].age);
}
// Query by ID and display information
int queryId;
printf("Enter the ID of the student to query: ");
scanf("%d", &queryId);
// Search for the student with the given ID
for (int i = 0; i < 5; i++) {
if (students[i].id == queryId) {
printf("Student found!\n");
printf("ID: %d\nName: %s\nAge: %d\n", students[i].id, students[i].name, students[i].age);
return 0; // Exit the program after finding the student
}
}
// If the program reaches this point, the student was not found
printf("Student with ID %d not found.\n", queryId);
return 0;
}
In this code:
We define a structure
Studentwith three members:id,name, andage.We declare an array of
Studentstructures to store information for multiple students.We use a loop to accept information for each student from the user.
We then prompt the user to enter the ID of the student they want to query and display the information if the student is found.
The code uses structures to organize and manage student information effectively.
We can use typedef to create an alias for the struct Student in the above code. This can make the code more readable and easier to maintain.
Here's how we can use typedef in the code:
#include <stdio.h>
// Define the structure for student and create an alias using typedef
typedef struct {
int id;
char name[50];
int age;
} Student;
int main() {
// Declare an array of structures to store student information
Student students[5];
// Accept information for each student
for (int i = 0; i < 5; i++) {
printf("Enter ID for student %d: ", i + 1);
scanf("%d", &students[i].id);
printf("Enter name for student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Enter age for student %d: ", i + 1);
scanf("%d", &students[i].age);
}
// Query by ID and display information
int queryId;
printf("Enter the ID of the student to query: ");
scanf("%d", &queryId);
// Search for the student with the given ID
for (int i = 0; i < 5; i++) {
if (students[i].id == queryId) {
printf("Student found!\n");
printf("ID: %d\nName: %s\nAge: %d\n", students[i].id, students[i].name, students[i].age);
return 0; // Exit the program after finding the student
}
}
// If the program reaches this point, the student was not found
printf("Student with ID %d not found.\n", queryId);
return 0;
}
In this modified code, we use typedef to create an alias Student for the struct that defines the student's information. This simplifies the declaration of the array of structures and makes the code more concise.
Here's a sample program with nested structures related to students, along with detailed comments to explain the nested structure and its usage:
#include <stdio.h>
// Define the structure for the address of the student
struct Address {
char street[50];
char city[50];
char state[50];
int zipCode;
};
// Define the structure for the student
struct Student {
int id;
char name[50];
int age;
struct Address studentAddress; // Nested structure for student's address
};
int main() {
// Declare a structure variable of type Student
struct Student student1;
// Accept information for the student
printf("Enter ID for the student: ");
scanf("%d", &student1.id);
printf("Enter name for the student: ");
scanf("%s", student1.name);
printf("Enter age for the student: ");
scanf("%d", &student1.age);
printf("Enter street: ");
scanf("%s", student1.studentAddress.street);
printf("Enter city: ");
scanf("%s", student1.studentAddress.city);
printf("Enter state: ");
scanf("%s", student1.studentAddress.state);
printf("Enter zip code: ");
scanf("%d", &student1.studentAddress.zipCode);
// Display information about the student
printf("Student Information:\n");
printf("ID: %d\nName: %s\nAge: %d\nAddress: %s, %s, %s, %d\n", student1.id, student1.name, student1.age, student1.studentAddress.street, student1.studentAddress.city, student1.studentAddress.state, student1.studentAddress.zipCode);
return 0;
}
In this code:
We define a structure
Addressto represent the address of the student, including street, city, state, and zip code.We then define a structure
Studentthat includes the student's ID, name, age, and a nested structurestudentAddressrepresenting the student's address.In the
mainfunction, we accept information for the student and the student's address, and then display the information.
This nested structure allows us to organize related data (in this case, student information and address) hierarchically, making the code more readable and maintainable.