代码原型:`#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * longestCommonPrefix(char ** strs, int strsSize)
{
int i = 0,j = 0,k=0;
int len = strlen(strs[0]);
for(i=1;i<strsSize;i++)
{
if(strlen(strs[i-1]) < strlen(strs[i]))
{
len = strlen(strs[i-1]);
}
}
i = 0;
char *buff = (char *)malloc(len+1);
while(i<len)
{
for(k = 1; k < strsSize;k++)
{
if(*(strs[k-1]+i) != *(strs[k]+i))
{
break;
}
}
if(k == strsSize )
{
buff[j++] = *(strs[k-1]+i);
}
else
{
break;
}
i++;
}
buff[j++] = '\0';
return buff;
}
void len(char ** strs, int strsSize)
{
printf(“%c\n”,*(strs[0]+1));
}
int main()
{
char *a[3]= {{"flower"},{"flow"},{"flight"}};
char **P = a;
char *buff="1111243w";
buff = longestCommonPrefix(P,10);
/*printf("%d ",*(P[0]+1));
printf("%d ",*P);
printf("%d ",*P +1);
printf("%d ",*(P+1));
printf("%d ",*(P+2));*/
}`
核心原因:longestCommonPrefix(P,10);这个10的参数传递错误,导致后面函数的指针越界,访问了未知的地址。将10改成3就可以了。