Correct Answer: By passing the structure directly
Explanation: When a structure contains an array, you can pass the structure directly to a function as an argument. This allows the function to access the structure and its array members efficiently.
#include <stdio.h>
struct Book {
char title[50];
int year;
};
void displayBook(struct Book b) {
printf("Title: %s, Year: %d\n", b.title, b.year);
}
int main() {
struct Book book1 = {"The C Programming Language", 1978};
displayBook(book1);
return 0;
}
Correct Answer: Title: The C Programming Language, Year: 1978
Explanation: The `displayBook` function takes a structure `Book` as an argument and prints its `title` and `year` members. In `main`, a `Book` structure `book1` is initialized with values, and `displayBook(book1);` prints “Title: The C Programming Language, Year: 1978”.
Correct Answer: By passing a pointer to the structure
Explanation: To modify a structure containing an array passed to a function, you typically pass a pointer to the structure. This allows the function to directly modify the array or any other members of the structure.
#include <stdio.h>
#include
struct Product {
char name[30];
float price;
};
void updateProduct(struct Product *p, float newPrice) {
p->price = newPrice;
}
int main() {
struct Product prod = {"Laptop", 1200.50};
updateProduct(&prod, 1350.75);
printf("Product: %s, Price: %.2f\n", prod.name, prod.price);
return 0;
}
Correct Answer: Product: Laptop, Price: 1350.75
Explanation: The `updateProduct` function takes a pointer to a `Product` structure and updates its `price` member. In `main`, a `Product` structure `prod` is initialized with name “Laptop” and price 1200.50. After calling `updateProduct(&prod, 1350.75);`, the `price` is modified to 1350.75, resulting in “Product: Laptop, Price: 1350.75”.
Correct Answer: By passing a pointer to the structure
Explanation: When a structure contains a pointer, you pass a pointer to the structure as an argument to functions. This allows functions to access and modify the structure and the data pointed to by its members.
#include <stdio.h>
struct Player {
char name[20];
int *scores;
};
void printPlayerScores(struct Player *player) {
printf("%s's scores:\n", player->name);
for (int i = 0; i < 3; ++i) {
printf("%d ", player->scores[i]);
}
printf("\n");
}
int main() {
struct Player p1 = {"Alice", (int[]){85, 92, 78}};
printPlayerScores(&p1);
return 0;
}
85 92 78
0 0 0
Correct Answer: Alice’s scores:
85 92 78
Explanation: The `printPlayerScores` function takes a pointer to a `Player` structure and prints the player’s name followed by their scores. In `main`, a `Player` structure `p1` is initialized with name “Alice” and scores {85, 92, 78}. It prints “Alice’s scores: 85 92 78”.
Correct Answer: By passing a pointer to the structure
Explanation: When a structure contains a dynamically allocated array, you pass a pointer to the structure as an argument to functions. This allows functions to access and modify the structure and the dynamically allocated array.
#include <stdio.h>
#include
struct Team {
char name[30];
int *scores;
};
void printTeamScores(struct Team *team) {
printf("%s's scores:\n", team->name);
for (int i = 0; i < 4; ++i) {
printf("%d ", team->scores[i]);
}
printf("\n");
}
int main() {
struct Team t1 = {"Red Team", (int *)malloc(4 * sizeof(int))};
t1.scores[0] = 92;
t1.scores[1] = 88;
t1.scores[2] = 95;
t1.scores[3] = 87;
printTeamScores(&t1);
free(t1.scores);
return 0;
}
92 88 95 87
0 0 0 0
Correct Answer: Red Team’s scores:
92 88 95 87
Explanation: The `printTeamScores` function takes a pointer to a `Team` structure and prints the team’s name followed by their scores. In `main`, a `Team` structure `t1` is initialized with name “Red Team” and a dynamically allocated array `scores` of size 4. After assigning scores and printing, it prints “Red Team’s scores: 92 88 95 87”. The allocated memory is freed using `free(t1.scores);` at the end.
Correct Answer: To define a new data type for the structure
Explanation: `typedef` in C is used to create a new name (alias) for an existing data type, including structures. It allows you to define a new name that can be used to declare variables of that structure type more conveniently.
Correct Answer: `struct MyStruct *ptr;`
Explanation: To declare a pointer to a structure `MyStruct` in C, you use the syntax `struct MyStruct *ptr;`. This declares `ptr` as a pointer that can point to variables of type `MyStruct`.
Correct Answer: To calculate the size of a structure in bytes
Explanation: In C, the `sizeof` operator returns the size of a variable or data type in bytes. When used with a structure, `sizeof(structure_name)` gives the size of the entire structure, including padding bytes for alignment.
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p1 = {3, 7};
struct Point *ptr = &p1;
printf("Coordinates: (%d, %d)\n", ptr->x, ptr->y);
return 0;
}
Correct Answer: Coordinates: (3, 7)
Explanation: In the code, `p1` is a structure of type `Point` initialized with coordinates (3, 7). A pointer `ptr` of type `struct Point *` is initialized to point to `p1`. Using `ptr->x` and `ptr->y` accesses and prints the coordinates of `p1`, resulting in “Coordinates: (3, 7)”.
Correct Answer: `struct Book b = {“Introduction to C”};`
Explanation: When initializing a structure with a string member (`char` array), you can directly initialize it with the string enclosed in double quotes. This initializes the first member (`title`) and leaves the rest (like `year`) initialized to default values (0 for `int`).
Correct Answer: To calculate the offset of a structure member
Explanation: `offsetof` is a macro defined in `
#include <stdio.h>
struct Employee {
char name[30];
int age;
};
void printEmployee(struct Employee *emp) {
printf("Employee: %s, Age: %d\n", emp->name, emp->age);
}
int main() {
struct Employee empArray[3] = {{"Alice", 25}, {"Bob", 30}, {"Charlie", 28}};
for (int i = 0; i < 3; ++i) {
printEmployee(&empArray[i]);
}
return 0;
}
Employee: Bob, Age: 30
Employee: Charlie, Age: 28
Employee: , Age: 0
Employee: , Age: 0
Employee: , Age: 30
Employee: , Age: 28
Correct Answer: Employee: Alice, Age: 25
Employee: Bob, Age: 30
Employee: Charlie, Age: 28
Explanation: The `main` function initializes an array `empArray` of type `Employee` with three elements, each containing a name and age. The `printEmployee` function takes a pointer to an `Employee` structure and prints its `name` and `age`. The loop iterates over `empArray`, printing each employee's details, resulting in the correct output.
Correct Answer: To group different data types under a single name
Explanation: A `union` in C allows you to define a data type that can hold different types of data in the same memory location. Unlike structures, where each member has its own memory space, a `union` uses the same memory space for all its members.
Correct Answer: The size of a structure is equal to the sum of the sizes of its members
Explanation: In C, the size of a structure is determined by the sum of the sizes of its individual members. However, due to padding for alignment reasons, the actual size may be larger than the sum of member sizes.