Revision Questions - Pointers and Malloc

1. Consider the following program:

int main(void) {
    int a = 1;
    int b = 2;
    swap(a, b);
    assert(a == 2);
    assert(b == 1);
}

void swap(int a, int b) {
    int tmp = a;
    a = b;
    b = tmp;
}

(a) What does assert do?

(b) What is the intention of the program?

(c) Why doesn't the program work?

(d) How could the program be modified so that it works as intended?


2. Consider the following program:

int main(void) {
    int a[] = {1, 2};
    swap(a);
    assert(a[0] == 2);
    assert(a[1] == 1);
}

void swap(int arr[]) {
    int tmp = arr[0];
    arr[0] = arr[1];
    arr[1] = tmp;
}

(a) What is a in the main function?

(b) Why does this program work, whereas the program in Question 1 did not?

(c) What is arr in the swap function?


3. Consider the following program:

int main(void) {
    int a = 0;
    printf("%ld\n", sizeof(a));
    char *s = "hello";
    printf("%ld\n", sizeof(s));
    printf("%ld\n", sizeof("hello"));
}

(a) What does sizeof do?

(b) What would the program print?


4. Consider the following code:

typedef int Integer;

typedef struct point {
    int x;
    int y;
} Point;

(a) What does the typedef keyword do?

(b) Explain the meaning of each of the typedefs.


5. Consider the following linked list node definition:

struct node {
    int value;
    struct node *next;
};

Which of the following calls to malloc correctly allocates memory for a linked list node?

(A) struct node n = malloc(sizeof(n));

(B) struct node n = malloc(sizeof(*n));

(C) struct node n = malloc(sizeof(struct node));

(D) struct node n = malloc(sizeof(struct node *));

(E) struct node *n = malloc(sizeof(n));

(F) struct node *n = malloc(sizeof(*n));

(G) struct node *n = malloc(sizeof(struct node));

(H) struct node *n = malloc(sizeof(struct node *));


6. Now add the following typedef to the code in the previous question:

typedef struct node Node;

(a) What does the typedef do?

(b) Now which of the following calls to malloc correctly allocates memory for a linked list node?

(A) Node n = malloc(sizeof(n));

(B) Node n = malloc(sizeof(*n));

(C) Node n = malloc(sizeof(Node));

(D) Node n = malloc(sizeof(Node *));

(E) Node n = malloc(sizeof(struct node));

(F) Node n = malloc(sizeof(struct node *));

(G) Node *n = malloc(sizeof(n));

(H) Node *n = malloc(sizeof(*n));

(I) Node *n = malloc(sizeof(Node));

(J) Node *n = malloc(sizeof(Node *));

(K) Node *n = malloc(sizeof(struct node));

(L) Node *n = malloc(sizeof(struct node *));

(c) What if the typedef was written like this instead?

typedef struct node *Node;


7. How can you allocate an array of n integers using malloc? How about an array of n characters?


8. Consider the following program:

int main(void) {
    char *s1 = "keen";
    char *s2 = cloneString(s1);
    s2[0] = 'b';
    printf("%s %s\n", s1, s2);
}

char *cloneString(char *s) {
    char *t = s;
    return t;
}

(a) What is s1 ?

(b) What is the intention of the program?

(c) Why doesn't the program work?

(d) How could the program be modified so that it works as intended?


9. Consider the following function that appends a node to a linked list:

struct node *append(struct node *list, int value) {
    struct node *n = malloc(sizeof(struct node));
    n->value = value;
    n->next = NULL;

    if (list == NULL) {
        return n;
    }

    struct node *curr = list; // <-- A
    while (curr->next != NULL) {
        curr = curr->next;
    }
    curr->next = n;
    return list;
}

Why doesn't curr require a malloc? That is, instead of the line marked with A , why don't we need a malloc like so?

    ...
    struct node *curr = malloc(sizeof(struct node));
    curr = list;
    ...


10. We can easily create 2D arrays local to a function like so:

int arr[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9},
};

Unfortunately we can't create the array like this if we want the array to persist outside of the function. How can we allocate a 2D array using malloc?

Resource created Wednesday 25 May 2022, 09:53:00 PM, last modified Saturday 04 June 2022, 07:14:30 PM.


Back to top

COMP2521 22T2 (Data Structures and Algorithms) is powered by WebCMS3
CRICOS Provider No. 00098G