关于二维数组的传参问题
为什么这道题使用 const char dict[]作为参数就能通过,而使用 const char **dict就会报错呢?参数在函数签名中:void recur(char **pins, const char *observed, const char dict[], 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 ' 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[],
char *word, int cur, int len, int *index) {
char temp;
sprintf(temp, "%s", word);
if(observed) {
for(int k = 0; dict - '0']; k++) {
printf("%d %d\n", cur, k);
temp = dict - '0'];
recur(pins, observed, dict, temp, cur + 1, len, index);
}
} else {
temp = '\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[] = DICT;
char temp;
*count = 1;
for(int i = 0; i < len; i++) *count *= strlen(dict - '0']);
char **pins = malloc(*count * sizeof(char *));
for(size_t i = 0; i < *count; i++) pins = calloc(len + 1, sizeof(char));
for(int i = 0, k = 0; dict - '0']; k++) {
temp = dict - '0'];
recur(pins, observed, dict, temp, i + 1, len + 1, &index);
}
return (const char **)pins;
}
void free_pins(const char ** pins) {
free(pins);
}
因为这两个类型是不一样的,是不同的类型
页:
[1]