48.8k views
1 vote
Write code to accomplish each of the following:

a) define a struct called part containing unsigned int variable partnumber and char array partname with values that may be as long as 25 characters (including the terminating null character).
b) define part to be a synonym for the type struct part.
c) use part to declare variable a to be of type struct part, array b[10] to be of type struct part and variable ptr to be of type pointer to struct part.
d) read a part number and a part name from the keyboard into the individual members of variable a.
e) assign the member values of variable a to element 3 of array b.
f) assign the address of array b to the pointer variable ptr.
g) print the member values of element 3 of array b using the variable

1 Answer

5 votes

Final answer:

The provided code defines a struct named 'part', sets up a synonym, declares variables, reads user input, assigns values to an array, and uses a pointer to print element values.

Step-by-step explanation:

To complete the tasks outlined in your question related to structures in C programming, here is the code that accomplishes each:

Part A: Defining the Structure

struct part {
unsigned int partnumber;
char partname[26];
};

Part B: Defining a Synonym

typedef struct part part;

Part C: Declaring Variables

part a;
part b[10];
part *ptr;

Part D: Reading from Keyboard

#include
scanf("%u%25s", &a.partnumber, a.partname);

Part E: Assigning Values

b[3] = a;

Part F: Address Assignment

ptr = b;

Part G: Printing Values

printf("Part number: %u\\Part name: %s\\", ptr[3].partnumber, ptr[3].partname);

Make sure to include the necessary headers, such as <stdio.h>, and handle user input carefully. Validate the input where applicable to ensure robustness of the code.

User Mark Carey
by
8.5k points