Each of the following descriptions represents a datatype. For each one, using C
ID: 3905486 • Letter: E
Question
Each of the following descriptions represents a datatype. For each one, using C code:
I. Declare the datatype.
II. Declare one variable having that datatype (not a pointer to it).
III. Initialise the variable. (The actual values are your choice. The simplest possible
initialisation code will suffice, but you must completely initialise the variable.)
Choose any valid names, where names have not already been given.
(a) An enum called Suit with the possible values spades, hearts, diamonds and
clubs, having corresponding integer values 1, 2, 3 and 4.
(b) A struct containing (1) a 5 by 3 array of real numbers, and (2) a pointer to a function that
takes an integer parameter and returns a string.
(c) A self-referential struct, containing two pointers (called left and right) to other instances
of the same struct, as well as a pointer to anything.
(d) A union that can store either (1) the enum in part (a), (2) the struct in part (b), or (3) the
struct in part (c).
(e) A union that can store either (1) a pointer to a constant integer, (2) a real number, or (3) a
struct containing both a pointer to a constant integer and a real number.
Explanation / Answer
(a)
enum Suit
{
spades=1,hearts,diamonds,clubs
};
void main()
{
enum Suit s=clubs;
printf("%d",s);
}
(b)
struct Sample
{
double array[5][3];
char* (*ptr)(int);
};
(c)
struct Node
{
struct Node *left; //variable pointing to structure itself
struct Node *right;
int *data;
};
(d)
union Details1
{
enum Suit s;
struct Node n;
};
(e)
struct Details2
{
const int *ptr;
double n;
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.