0

Hi, I'm working on some questions for homework and I have a small idea what the answers may be. Can anyone look my answers over and help me out maybe explaining why they may be wrong or right?

Question 1: answer "b"
Here is a diagram of memory:
    Which declaration would generate this ?

    f: [[char*][int]] [[char*][int]] [[char*][int]]   // <- assume 3 items contiguous in mem

    typedef struct
    {
        char *name;
        int ssn;
    } Foo;

    a) Foo f[3];

    B) Foo *f[3];

    c) Foo **f[3];

Question 2: answer "a"
Assume this declaration:    int * arr;

    Which diagram best describes memory allocation ?   (CIRCLE ANSWER)

    a)  arr: [int*]

    B)  arr: [int*] --> [NULL]

    c)  arr: [int] --> [NULL]

    d)  arr: [NULL]

Question 3: answer "a"
Assume this declaration:    int ** arr;

    Which diagram best describes memory allocation ?   (CIRCLE ANSWER)

    a)  arr: [int**]

    B)  arr: [int*] --> [int*]

    c)  arr: [int*] --> [int]

    d)  arr: [NULL]

Question 4: answer "b"
Assume this declaration:    int * arr[5];

    Which diagram best describes memory allocation ?   (CIRCLE ANSWER)

    a)  arr: [0][0][0][0][0]

    B)  arr: [NULL][NULL][NULL][NULL][NULL]

    c)  arr: [int*][int*][int*][int*][int*]

    d)  arr: [5]
2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by Gonbe
0

I think the memory representation "images" are vague but I'll try anyway:

1.
Answer should be "a". You answered B. That would result in an array consisting of 3 pointers to "Foo" objects in memory, so that would be

[Foo][Foo][Foo*]

according to the notation used in your example. (I think)

2.
You answered "a" which is correct. Although I think the options are a bit vague.. (What is 'c' even supposed to mean??)
When you declare "arr" like that it is an int pointer but you cannot really say something about its value; they are not automatically initialized. (though sometimes they are,see below)


  1. Again I don't quite understand the "images" but I would choose "a" with some assumptions about what those arrows mean. arr points to an int pointer but you cannot say anything about the value of arr.

4.
Answer would be "c". You answered "b" but the pointers the array consists of are not automatically set to NULL.

Some of these questions can have multiple awnsers depending on how the things are declared. Global variables and static variables for example are automatically initialized to zero.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.