|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么这道题使用 const char dict[][6]作为参数就能通过,而使用 const char **dict就会报错呢?
参数在函数签名中:void recur(char **pins, const char *observed, const char dict[][6], char *word, int cur, int len, int *index)
报错信息:
- Test Crashed
- Caught unexpected signal: SIGSEGV (11). Invalid memory access.
- solution.c:41:31: warning: incompatible pointer types passing 'char [10][6]' to parameter of type 'const char **' [-Wincompatible-pointer-types]
- recur(pins, observed, dict, temp, i + 1, len + 1, &index);
- ^~~~
- solution.c:8:60: note: passing argument to parameter 'dict' here
- void recur(char **pins, const char *observed, const char **dict,
- ^
- 1 warning generated.
复制代码
题目:https://www.codewars.com/kata/5263c6999e0f40dee200059d/train/c
代码:
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #define DICT {"08", "124", "2135", "326", "4517", "54628", "6539",\
- "784", "87950", "986"}
- void recur(char **pins, const char *observed, const char dict[][6],
- char *word, int cur, int len, int *index) {
- char temp[len];
- sprintf(temp, "%s", word);
-
- if(observed[cur]) {
- for(int k = 0; dict[observed[cur] - '0'][k]; k++) {
- printf("%d %d\n", cur, k);
- temp[cur] = dict[observed[cur] - '0'][k];
- recur(pins, observed, dict, temp, cur + 1, len, index);
- }
- } else {
- temp[len - 1] = '\0';
- sprintf(pins[*index], "%s", temp);
- (*index)++;
- }
- }
- const char** get_pins(const char* observed, size_t* count) {
- int len = strlen(observed), index = 0;
-
- char dict[][6] = DICT;
- char temp[len + 1];
-
- *count = 1;
- for(int i = 0; i < len; i++) *count *= strlen(dict[observed[i] - '0']);
- char **pins = malloc(*count * sizeof(char *));
- for(size_t i = 0; i < *count; i++) pins[i] = calloc(len + 1, sizeof(char));
-
-
- for(int i = 0, k = 0; dict[observed[i] - '0'][k]; k++) {
- temp[0] = dict[observed[i] - '0'][k];
- recur(pins, observed, dict, temp, i + 1, len + 1, &index);
- }
- return (const char **)pins;
- }
-
- void free_pins(const char ** pins) {
- free(pins);
- }
复制代码
|
|