0% found this document useful (0 votes)
34 views4 pages

Structure Problem Solutions

The document provides solutions to various C programming problems involving structures. It covers defining and printing structures, using arrays of structures, nested structures, pointers to structures, and passing structures by reference. Additionally, it includes examples of dynamic memory allocation and using typedef with structures.

Uploaded by

heyyanshu.07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views4 pages

Structure Problem Solutions

The document provides solutions to various C programming problems involving structures. It covers defining and printing structures, using arrays of structures, nested structures, pointers to structures, and passing structures by reference. Additionally, it includes examples of dynamic memory allocation and using typedef with structures.

Uploaded by

heyyanshu.07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Solutions to C Structure Programming Problems

Problem 1: Define and print a structure


struct Student {
char name[30];
int roll;
float marks;
};
int main() {
struct Student s;
scanf("%s %d %f", s.name, &s.roll, &s.marks);
printf("%s %d %.2f", s.name, s.roll, s.marks);
}

Problem 2: Array of Structures


struct Employee {
char name[30];
int id;
float salary;
};
int main() {
struct Employee e[5];
for(int i=0;i<5;i++)
scanf("%s %d %f", e[i].name, &e[i].id, &e[i].salary);
for(int i=0;i<5;i++)
printf("%s %d %.2f\n", e[i].name, e[i].id, e[i].salary);
}

Problem 3: Nested Structures


struct Address {
char city[20];
int pin;
};
struct Student {
char name[20];
int roll;
struct Address addr;
};
int main() {
struct Student s;
scanf("%s %d %s %d", s.name, &s.roll, s.addr.city, &s.addr.pin);
printf("%s %d %s %d", s.name, s.roll, s.addr.city, s.addr.pin);
}

Problem 4: Pointer to Structure


struct Book {
char title[30];
char author[30];
Solutions to C Structure Programming Problems

float price;
};
int main() {
struct Book b, *ptr;
ptr = &b;
scanf("%s %s %f", ptr->title, ptr->author, &ptr->price);
printf("%s %s %.2f", ptr->title, ptr->author, ptr->price);
}

Problem 5: Structure with Function


struct Car {
char brand[20];
char model[20];
int year;
};
void display(struct Car c) {
printf("%s %s %d", c.brand, c.model, c.year);
}
int main() {
struct Car c;
scanf("%s %s %d", c.brand, c.model, &c.year);
display(c);
}

Problem 6: Structure Array + Function


struct Product {
int id;
char name[20];
float price;
};
void input(struct Product p[], int n) {
for(int i=0;i<n;i++)
scanf("%d %s %f", &p[i].id, p[i].name, &p[i].price);
}
void output(struct Product p[], int n) {
for(int i=0;i<n;i++)
printf("%d %s %.2f\n", p[i].id, p[i].name, p[i].price);
}
int main() {
struct Product p[5];
input(p, 5);
output(p, 5);
}

Problem 7: Compare Two Structures


struct Date {
int day, month, year;
Solutions to C Structure Programming Problems

};
int main() {
struct Date d1, d2;
scanf("%d %d %d", &d1.day, &d1.month, &d1.year);
scanf("%d %d %d", &d2.day, &d2.month, &d2.year);
if (d1.day==d2.day && d1.month==d2.month && d1.year==d2.year)
printf("Equal");
else
printf("Not Equal");
}

Problem 8: Structure with typedef


typedef struct {
char name[20];
int roll;
float marks;
} Student;

int main() {
Student s;
scanf("%s %d %f", s.name, &s.roll, &s.marks);
printf("%s %d %.2f", s.name, s.roll, s.marks);
}

Problem 9: Structure with dynamic memory


#include <stdlib.h>
struct Person {
char *name;
};
int main() {
struct Person p;
p.name = (char *)malloc(30 * sizeof(char));
scanf("%s", p.name);
printf("Name: %s", p.name);
free(p.name);
}

Problem 10: Pass Structure by Reference


struct Employee {
char name[20];
int age;
};
void update(struct Employee *e) {
e->age += 1;
}
int main() {
struct Employee e;
Solutions to C Structure Programming Problems

scanf("%s %d", e.name, &e.age);


update(&e);
printf("%s %d", e.name, e.age);
}

You might also like