繁体   English   中英

分配字符数组和字符串

[英]Allocate char array and strings

我在理解下面的代码时遇到问题。

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

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

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

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

#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/上找到

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

index = strlen(strs[0]);

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

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

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

str[i][j]

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

对于初学者来说,该程序很糟糕且无效。:)

例如,参数strs指向的一维数组的第一个元素的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指向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'等等.

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

#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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM