【问题标题】:Allocate char array and strings分配 char 数组和字符串
【发布时间】:2019-08-14 13:47:23
【问题描述】:

我无法理解下面的代码。

  1. index=strlen(strs[0]) 得到什么值?

  2. char *a= malloc (sizeof(char)*(index+1))这是为char数组分配数组的标准方式吗?

  3. strs[i][j] 代表什么?

这是我在 leetcode 上找到的代码。只是试图理解代码。 (来自 sanghi 用户在 leetcode 上的代码)

#include<string.h>
char* longestCommonPrefix(char** strs, int strsSize) 
{
    int i=0; int j=0;int index;int tempindex=0;
    if(strsSize<1)
    return "";
    index=strlen(strs[0]);
    char *a;
    a= malloc(sizeof(char)*(index+1));
    strcpy(a,strs[0]);
    for(i=1;i<strsSize;i++)
    {   tempindex=0;
        for(j=0;j<index;j++)
        { 
            if(a[j]==strs[i][j])
            tempindex++;
            else
            {a[j]='\0';
             break;
            }
        } 
          if (tempindex==0)return ("");
          if(tempindex<index)index=tempindex;

    }
    return a;

}

预期结果可以在https://leetcode.com/problems/longest-common-prefix/找到

【问题讨论】:

  • 在调试器中运行它并检查您感兴趣的值。
  • 在我看来,您需要花时间和金钱来阅读一些书籍。我不推荐从在线评委/竞赛网站学习(我强烈推荐除了此类网站之外的任何其他网站)..

标签: c arrays character c-strings strlen


【解决方案1】:

strs 是一个字符串数组。 strsSize 是数组中的字符串数。

index = strlen(strs[0]);

这只是获取数组中第一个字符串strs[0] 的长度。

a = malloc(sizeof(char)*index+1);

这将分配足够的内存来存储相同大小的字符串。我说足够的内存是因为每个字符串实际上都有长度 + 1 个字符。最后一个字符是\0,一个空终止符。您始终必须确保终止您的字符串,否则可能会发生一堆奇怪的缓冲区溢出问题。

str[i][j]

这将访问数组中第 i 个字符串中的第 j 个字符。

【讨论】:

  • "strs 是一个字符串数组" 不,它是一个指向char 的指针,一个char**。指针不是数组,也不是 1D,也不是 2D,也不是 .... 你简化得太多了。
【解决方案2】:

对于初学者来说,该程序是错误且无效的。:)

例如,参数strs 指向的一维数组第一个元素的大小应具有size_t 类型而不是int。

所有其他处理索引的变量也应具有size_t 类型,例如

size_t index = strlen( strs[0] );

因为标准 C 函数 strlen 具有返回类型 size_t。

函数中的源数组没有改变,所以第一个参数应该用限定符const声明。

就是函数声明应该是这样的

char * longestCommonPrefix( const char** strs, size_t strsSize); 

数组的元素(字符串)可以有不同的长度,所以这个循环

for(j=0;j<index;j++)

具有未定义的行为,因为数组的某些元素(字符串)的长度可能小于变量 index 的值。

实际上不需要计算数组元素的长度。循环可以使用条件

for( j=0; j < index && strs[i][j] != '\0'; j++)

此外,由于 if 语句中的 return 子语句,该函数存在内存泄漏

a= malloc(sizeof(char)*(index+1));
//...
if (tempindex==0)return ("");

即指针a指向的分配内存不会释放。

index=strlen(strs[0]) 得到什么值?

index 获取存储在字符串数组第一个元素中的字符串长度。

例如,如果你有一个数组

char *strs[] = { "Hello", "Bye", "Good Morning" };

然后将index设置为字符串"Hello"的长度。

char a= malloc (sizeof(char)(index+1)) 这是标准的方法吗 为char数组分配数组?

是的,在这个声明中,分配了一个足够大的内存来存储strs指向的数组的第一个元素的字符串(包括它的终止零)。

strs[i][j] 代表什么?

strs[i][j]访问strs指向的数组的第i个元素的第j个字符。

例如上面的声明strs[0][0]等于'H',strs[0][1]等于'e',strs[1][0]等于'B'等等。

附:定义函数的更好方法如下,如演示程序所示。

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

size_t longestCommonPrefix( const char **strs, size_t strsSize )
{
    size_t n = 0;

    if ( strsSize != 0 )
    {
        n = strlen( *strs );

        for ( size_t i = 1; n != 0 && i < strsSize; i++ )
        {
            size_t j = 0;

            while ( j < n && strs[i][j] == strs[i-1][j] ) j++;

            if ( j < n ) n = j;
        }
    }

    return n;
}

int main(void) 
{
    char * strs[] = { "0123456789", "012345", "0123" };

    size_t n = longestCommonPrefix( ( const char ** )strs, sizeof( strs ) / sizeof( *strs ) );

    char *p = NULL;

    if ( n != 0 )
    {
        p = malloc( n + 1 );
        memcpy( p, strs[0], n );
        p[n] = '\0';

        printf( "The longest common prefix is \"%s\"\n", p );
    }

    free( p );

    return 0;
}

程序输出是

The longest common prefix is "0123"

【讨论】:

    猜你喜欢
    • 2011-02-07
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2012-10-16
    • 2012-05-03
    相关资源
    最近更新 更多