鱼C论坛

 找回密码
 立即注册
查看: 2617|回复: 1

[已解决]关于二维数组的传参问题

[复制链接]
发表于 2022-9-3 23:08:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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)

报错信息:
  1. Test Crashed
  2. Caught unexpected signal: SIGSEGV (11). Invalid memory access.

  3. solution.c:41:31: warning: incompatible pointer types passing 'char [10][6]' to parameter of type 'const char **' [-Wincompatible-pointer-types]
  4.         recur(pins, observed, dict, temp, i + 1, len + 1, &index);
  5.                               ^~~~
  6. solution.c:8:60: note: passing argument to parameter 'dict' here
  7. void recur(char **pins, const char *observed, const char **dict,
  8.                                                            ^
  9. 1 warning generated.
复制代码




题目:https://www.codewars.com/kata/5263c6999e0f40dee200059d/train/c

代码:
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>

  4. #define DICT {"08", "124", "2135", "326", "4517", "54628", "6539",\
  5. "784", "87950", "986"}

  6. void recur(char **pins, const char *observed, const char dict[][6],
  7.            char *word, int cur, int len, int *index) {
  8.     char temp[len];
  9.     sprintf(temp, "%s", word);
  10.   
  11.     if(observed[cur]) {
  12.         for(int k = 0; dict[observed[cur] - '0'][k]; k++) {
  13.             printf("%d %d\n", cur, k);
  14.             temp[cur] = dict[observed[cur] - '0'][k];
  15.             recur(pins, observed, dict, temp, cur + 1, len, index);
  16.         }
  17.     } else {
  18.         temp[len - 1] = '\0';
  19.         sprintf(pins[*index], "%s", temp);
  20.         (*index)++;
  21.     }
  22. }

  23. const char** get_pins(const char* observed, size_t* count) {
  24.     int len = strlen(observed), index = 0;
  25.   
  26.     char dict[][6] = DICT;
  27.     char temp[len + 1];
  28.    
  29.     *count = 1;
  30.     for(int i = 0; i < len; i++) *count *= strlen(dict[observed[i] - '0']);

  31.     char **pins = malloc(*count * sizeof(char *));
  32.     for(size_t i = 0; i < *count; i++) pins[i] = calloc(len + 1, sizeof(char));
  33.    
  34.   
  35.     for(int i = 0, k = 0; dict[observed[i] - '0'][k]; k++) {
  36.         temp[0] = dict[observed[i] - '0'][k];
  37.         recur(pins, observed, dict, temp, i + 1, len + 1, &index);
  38.     }
  39.     return (const char **)pins;
  40. }
  41.   

  42. void free_pins(const char ** pins) {
  43.    free(pins);
  44. }
复制代码


最佳答案
2022-9-3 23:17:07
因为这两个类型是不一样的,是不同的类型
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-9-3 23:17:07 | 显示全部楼层    本楼为最佳答案   
因为这两个类型是不一样的,是不同的类型
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 16:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表