Question
2. (Union) Consider union { char c; int i; } u; u.c = A; Draw a memory picture for variable u. The size of each
2. (Union) Consider union {
char c;
int i;
} u;
u.c = A;
Draw a memory picture for variable u.
The size of each of your memory unit must be one byte. You should indicate clearly the member names of u in your picture.
3. (Enumeration) Give an example of an enumeration type and declare a variable of that type.
4. (Memory allocation) Consider
typedef struct {
char name[20];
int balance;
int gain;
} Player;
int n;
Player *p;
scanf(%d, &n);
Complete the code above by allocating a memory for n players each of which of type Player and making p point to the start of that memory (or in other words, making p have the value of the starting address of that memory). [Recall the slides on dynamic memory allocation function malloc.]
5. (Linked Lists) Consider
typedef strut node {
char name[20];
struct node *next;
} Node;
Write a C function with the following prototype
Node *find(char *name, Node *head, Node *tail)
where name is a string, head points to the first node of a linked list and tail points to the last node of the linked list. The function returns the address of the first node whose name is equal to the string in name. It returns NULL if no node of the linked list contain name. No standard library function is allowed except those for strings.
6. (Abstract Data Type) Consider the data structure for the stack ADT:
typedef struct node {
int n;
struct node *next; } Node;
struct stackHead { Node *head; };
typedef stackHead *Stack;
1. Stack s;
2. s = create();
3. push(s, 1);
For functions create and push, refer to their pseudocode in lecture notes. After EACH statement (1 to 3) above, draw a diagram of all memory relevant to s. If the content in the relevant memory unit is unknown, put ? inside the unit, otherwise, put the right information there. As for pointers/memory addresses, you can either make up some addresses or use an arrow to indicate their values.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started