Problems with the 'secret handshake' exercise in C

,

I’m trying to solve this exercise, but I keep getting the same error every time. At first, I thought it was my code, but I made several corrections and the error persisted. I was using the CLI, but I also tried the Exercism web compiler and got the same error.

Screenshot_3

Here is the link to my code:

Please share your code here using a codeblock. Mentoring requests can only be accepted once.

secret_handshake.c

#include "secret_handshake.h"

// Lista das ações correspondentes aos bits 0..3
static const char* actions[] = { "wink", "double blink", "close your eyes", "jump" };

// Função que retorna o array de ações baseado no número fornecido
const char **commands(size_t number) {
    // Array estático para retornar (4 ações + NULL)
    static const char* result[5];
    int count = 0;

    // Verifica cada bit dos 4 primeiros (0..3)
    for (int i = 0; i < 4; i++) {
        if (number & (1 << i)) {
            result[count++] = actions[i]; // Adiciona a ação correspondente
        }
    }

    // Se o 5º bit (16) estiver setado, inverte a ordem
    if (number & 0x10) {
        for (int i = 0; i < count / 2; i++) {
            const char* tmp = result[i];
            result[i] = result[count - 1 - i];
            result[count - 1 - i] = tmp;
        }
    }

    // Termina o array com NULL
    result[count] = NULL;

    return result;
}

secret_handshake.h


#ifndef SECRET_HANDSHAKE_H
#define SECRET_HANDSHAKE_H

#include <stddef.h>

// Função que recebe um número e retorna um array de strings
// representando as ações do "secret handshake".
// O array termina com NULL para indicar o fim.
const char **commands(size_t number);

#endif

You should post the code here using codeblocks:

```
code inside

```

This link there is a private request for mentorship. If you are asking for help in a public forum, then it’s not private, right? You should enter the queue.

EDIT: Just saw Isaac’s comment =)

1 Like

What is the end value for count? Where is it used?

Also, most of C exercises which expect an array returned call free on it, so you shouldn’t use a static array. I’ve just checked it and the same happens here.

count is the number of actions in the handshake. It is used to allocate the correct size for the result array and to copy only the actions that apply. This ensures the array is dynamically allocated so it can be safely freed.

Yes, I use malloc to create the result array dynamically, so the caller can safely free it. No static array is used.

A dynamic array is one allocated on the heap, typically using malloc or calloc. For a reference, you can check cpp reference.

You are using a static array (result), one allocated in the stack.

2 Likes

Alright, I’ll give it a shot. If that does the trick, we can call this case closed.

After a long 38 attempts, I finally managed to solve the problem. I’ll share the code with you all."

secret_handshake.c

#include "secret_handshake.h"
#include <stdlib.h>
const char *actions[MAX_ACTIONS] = {
    "wink",
    "double blink",
    "close your eyes",
    "jump"
};
const char **commands(int number) {
    // aloca espaço para até 4 ações + terminador NULL
    const char **result = calloc(MAX_ACTIONS + 1, sizeof(char *));
    if (!result) {
        return NULL;
    }
    int idx = 0;
    // verifica bits 0–3
    for (int i = 0; i < MAX_ACTIONS; i++) {
        if (number & (1 << i)) {
            result[idx++] = actions[i];
        }
    }
    // se bit 4 (16) está setado, inverte
    if (number & (1 << 4)) {
        for (int i = 0; i < idx / 2; i++) {
            const char *tmp = result[i];
            result[i] = result[idx - 1 - i];
            result[idx - 1 - i] = tmp;
        }
    }
    // já está garantido que result[idx] == NULL por causa do calloc
    return result;
}

secret.handshake.h

* #ifndef SECRET_HANDSHAKE_H

* #define SECRET_HANDSHAKE_H

* #define MAX_ACTIONS 4

* const char **commands(int number);

* #endif

Thank you for your guidance.

2 Likes