struct personT {
char *name;
int age;
};
int main() {
struct personT me, *you;
me.name = malloc(sizeof(char)*10);
strcpy(me.name,"Tia");
me.age = 50;
you = malloc(sizeof(struct personT));
you->name = malloc(sizeof(char)*10);
strcpy(you->name, "Elmo");
you->age = 40;
}
The contents of memory at the end of main:
ON THE STACK ON THE HEAP
============= ===========
-------- ----------------
me | *--|---------------------------->| "Tia" |
| 50 | ----------------
--------
------ -------- ----------------
you | *-|------------------------------->| *--|------->| "Elmo" |
------ | 40 | ----------------
--------
What is the type and value of each of the following expressions
(and are they all valid?):
EXPRESSION TYPE VALUE
---------- ---- ------
1. me struct personT (base address of "Tia" string in heap, 50)
2. you struct personT * base address of personT struct in heap
3. me.name char * base address of "Tia" string in heap
4. you.name INVALID INVALID (you is a pointer not a struct)
5. me.name[2] char 'a'
6. you->name[2] char 'm'