Question

Hey all this is for C programming.

Here are the questions as they were presented to me, can someone tell me how I would correctly write code for the following statements in C? FYI i already answered this quiz but my professor won't post the answers himself. I did very poorly when he graded it, but to help make sense of this i will provide my answers (as incorrect as they are)

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

Use typedef to name this new data type: mtrStatus_t

typedef unsigned char mtrStatus_t;

struct motorStatus mtrStatus_t {
    mtrStatus_t count: 4;
    mtrStatus_t redLED: 1;
    mtrStatus_t greenLED: 1;
    mtrStatus_t motorDirection: 2;
};

2: Create a new instance of the structure and name it motor1Status

motorStatus = motor1Status;

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

Was it helpful?

Solution

For the first one I would do something like this:

typedef struct motorStatus  
{
    int count: 4;
    int redLED: 1;
    int greenLED: 1;
    int motorDirection: 2;
} mtrStatus_t;

The second one is more like:

mtrStatus_t motor1status;

and finally:

motor1status.count = 0x9;
motor1status.redLED = 1;
motor1status.greenLED = 0;
motor1status.motorDirection = 0x02;

Count is a hex number because it is BCD (Binary coded decimal) http://en.wikipedia.org/wiki/Binary-coded_decimal In BCD, you use 4 bits to represent the numbers 0-9, there are some unused bit patterns, so the easy way to work with it is to just use hex (which also uses 4 bits to represent the numbers 0x0-0xf), but in BCD you just don't use the numbers 0xa-0xf.

The reason motorDirection is 0x02 is because he wants motor direction of 10, but it is a 2 bit field, so I am assuming that he means 10 binary, which is 0x02 hex.

OTHER TIPS

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top