leetcode刷题-c语言如何在函数中返回字符数组

题目:
编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 “”。

代码:

char* longestCommonPrefix(char** strs, int strsSize) {
            
    int n=strlen(strs[0]);
    static char a[n];
    int i;
    int j;
    for(i=0;i

结果:

solution.c: In function 'longestCommonPrefix'
Line 4: Char 17: error: storage size of 'a' isn't constant
     static char a[n];
                 ^

去掉static:

char* longestCommonPrefix(char** strs, int strsSize) {
            
    int n=strlen(strs[0]);
    char a[n];
    int i;
    int j;
    for(i=0;i

结果:
leetcode刷题-c语言如何在函数中返回字符数组_第1张图片

你可能感兴趣的:(菜狗学习日记)