力扣数组题”如果题解里面的第一个参数是char**,如果想用main函数去调用测试一下,main函数里面该怎么写啊

力扣数组题

img



如果题解里面的第一个参数 是char**,如果想用main函数去调用测试一下,main函数里面该怎么写啊?
我自己写了一个但是报错了


#include
#include
int prefixCount(char**words, int wordsSize, char* pref) {
    int res = 0;
    int len = strlen(pref);
    for (int i = 0; i < wordsSize; i++) {
       if (strncmp(words[i], pref,len) == 0) {
            res++;
        }
    }
    return res;
}
int main()
{
    char words[][10] = { "pay", "attention", "practice", "attend" };
    char pref[] = "code";
   printf("%d", prefixCount(words,4,pref));
}


#include<stdio.h>
#include<string.h>
int prefixCount(char**words, int wordsSize, char* pref) {
    int res = 0;
    int len = strlen(pref);
    for (int i = 0; i < wordsSize; i++) {
       if (strncmp(words[i], pref,len) == 0) {
            res++;
        }
    }
    return res;
}
int main()
{
    char *words[][10] = { "pay", "attention", "practice", "attend" };
    char pref[] = "at";
    char** p = (char**)words;    
   printf("%d", prefixCount(p,4,pref));
}

这样写即可,望采纳!


#include<stdio.h>
#include<string.h>

int prefixCount(char** words, int wordsSize, char* pref)
{
    int res = 0;
    int len = strlen(pref);
    for (int i = 0; i < wordsSize; i++)
    {
       if (strncmp(words[i], pref, len) == 0)
       {
            res++;
       }
    }
    return res;
}

int main()
{
    char* words[] = { "pay", "attention", "practice", "attend" };
    char* pref = "code";

    printf("Number of strings with prefix '%s': %d\n", pref, prefixCount(words, 4, pref));

    return 0;
}

帮你改了代码