【问题标题】:Create a 2d array of strings using dynamically allocation in c在c中使用动态分配创建一个二维字符串数组
【发布时间】:2020-09-08 12:08:24
【问题描述】:

我必须存储一些在 c 代码的 args 中给出的字符串。我遍历它们,但我无法正确存储它们,因为我不知道它们的长度,也不知道它们的数量。更好的方法应该是一个二维指针数组,这样我就可以为每个新字符串动态分配内存。问题是我是 c 新手,我对该技术有很多困惑。我尝试初始化双指针并使用函数插入元素,它为另一列(新字符串)分配空间并设置长度(字符串大小)。

char** files;
int files_i=0;

void insert(char** root,char[] str)
{
    root=(char **)malloc(sizeof(char *));
    root[files_i]=(char *)malloc(sizeof(char)*sizeof(str));
    root[files_i*sizeof(str)]=str;
    i++;
} 

我将双指针和我需要“附加”的字符串传递给函数。它不起作用,我对如何迭代它也有很大的疑问......

【问题讨论】:

    标签: c arrays memory-management append c-strings


    【解决方案1】:
    1. 使用strlen(str) 而不是sizeof(str) 来计算字符串长度。
    root[files_i]= malloc(strlen(str) + 1); // +1 for null character at the end of the string
    if(!root[file_i]) {return;}
    
    1. 如果要复制字符串,请使用strcpy 而不是使用= 运算符。或者使用strdup(如果使用strdup,则不需要为字符指针分配内存)。
    strcpy(root[files_i],str); // copy string str to "file_i" position of array root
    
    1. 如果你使用全局计数器file_i,你应该使用realloc作为root,因为root的大小必须变化(我认为这是错字,i++应该改为file_i++? )。
    root= realloc(root, sizeof(char *) * (file_i + 1));
    // do not forget to check the return value of malloc or realloc function.
    if(!root) {return;}
    
    1. 不要强制转换mallocrealloc 函数。见Do I cast the result of malloc?

    【讨论】:

    • 您对所有事情都是正确和清楚的,我非常感谢您的帮助,并且我理解了很多这个话题。非常感谢!
    【解决方案2】:

    你需要的是以下内容

    char **files = NULL;
    size_t files_i = 0;
    
    //...
    
    int insert( char ***root, const char str[], size_t i )
    {
        char *p = malloc( strlen( str ) + 1 );
    
        int success = p != NULL;
    
        if ( success )
        {
            char **tmp = realloc( *root, ( i + 1 ) * sizeof( char * ) );
    
            if ( success )
            {
                strcpy( p, str );
                tmp[i] = p;
                *root = tmp;
            }
            else
            {
                free( p );
            }
        }
    
        return success;
    }
    

    然后在调用者中你可以写例如

    if ( insert( &files, some_string, files_i ) ) ++files_i;
    

    这是一个演示程序。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int insert( char ***root, const char str[], size_t i )
    {
        char *p = malloc( strlen( str ) + 1 );
    
        int success = p != NULL;
    
        if ( success )
        {
            char **tmp = realloc( *root, ( i + 1 ) * sizeof( char * ) );
    
            if ( success )
            {
                strcpy( p, str );
                tmp[i] = p;
                *root = tmp;
            }
            else
            {
                free( p );
            }
        }
    
        return success;
    }
    
    int main(void) 
    {
        char **files = NULL;
        size_t files_i = 0;
    
        if ( insert( &files, "Hello", files_i ) ) ++files_i; 
        if ( insert( &files, "World", files_i ) ) ++files_i; 
    
        for ( size_t i = 0; i < files_i; i++ )
        {
            puts( files[i] );
        }
    
        for ( size_t i = 0; i < files_i; i++ )
        {
            free( files[i] );
        }
        free( files );
    
        return 0;
    }
    

    它的输出是

    Hello
    World
    

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 2021-12-20
      • 2017-03-11
      • 1970-01-01
      • 2021-04-04
      • 2022-01-04
      • 1970-01-01
      • 2017-09-28
      相关资源
      最近更新 更多