Consider meeting these requirements in the order they are specified. Note that some requirements are merged together as one. This is often the case for assignments; Lecturers aren't always perfect linguists or logicians, particularly IT lecturers.
1: Create a structure that will hold bits the represent the following:
count: a 4 bit BCD number
red LED: 1 bit
green LED: 1 bit
motor direction: 2 bits
Name the structure motorStatus
Respond to this, first, without using typedef. Use the type int
for bitfields. Next requirement:
Use typedef to name this new data type: mtrStatus_t
You've demonstrated the ability to write basic typedefs. typedef unsigned char mtrStatus_t;
means "I define mtrStatus_t to alias unsigned char". Now, write a basic typedef like this that means "I define mtrStatus_t to alias struct motorStatus". Put it after the struct motorStatus
definition, so that the compiler can see what it's aliasing.
2: Create a new instance of the structure and name it motor1Status
To clarify, your lecturer is asking you to declare a variable named motor1Status
, which has the type mtrStatus_t
or struct motorStatus
. I presume you can declare variables, but correct me if I'm wrong.
3: Write statements to initialize the new structure members as follows:
count: 9 BCD
red LED: 1
green LED: 0
Motor Direction: 10
count: 0x09; redLED: 0x01; greenLED: 0x00 motorDirection: 0x0A
Your lecturer is asking for initialisation, not assignment. In char str[] = "fubar";
and char str[] = {'h','e','l','l','o','\0'};
, str is declared and initialised to store a string corresponding to "fubar". In char str[6]; strcpy(str, "fubar");
str is declared without an initialisation, and each byte in "fubar" is copied (assigned) to the corresponding positions in str. How do you initialise a struct? Very similarly to the second str initialisation.
struct length_prefixed_string {
size_t length;
int zero;
char string[];
};
/* Initialisation here: */
struct length_prefixed_string hello = { .length = 5,
.string = "hello" };
This example uses a flexible array member, which can only be declared at the end of a struct: char string[];
. It conveys an array of unknown length located at the end of the struct, which isn't counted towards sizeof (struct length_prefix_string)
. For this reason, the example stores the unknown length in the length argument. This is a little off-topic, but you can see an initialisation (as opposed to an assignment) above. This example differentiates initialisation from assignment; You can't assign the .string
member of this struct with hello.string = "hello";
.
Another difference is: Any members not mentioned by the initialisation will be assigned a zero value (but only when an initialisation is present), hence the value of hello.zero
in the above example will be 0. For this reason, you can declare an array and zero-fill it with one statement: int array[size] = { 0 };
(another difference between assignment and initialisation: int array[size]; array = { 0 };
is invalid). Having said that, it'd be a good idea to ignore this fact for the purpose of your task, and explicitly initialise greenLED so that your marking examiner can't misunderstand.
I do hope I was helpful, here...