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.
#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
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.
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.