【问题标题】:Return the array from a struct c language从 struct c 语言返回数组
【发布时间】:2021-07-07 07:13:41
【问题描述】:

我正在尝试返回结构内的数组。

我必须在 struct integer_array 中创建一个新数组,然后在最后返回该数组。

任务是取一个字符串数组并计算每个索引以及它的大小。

所以是一个数组

["This", "is", "the", "way"]

应该返回:

[4, 2, 3, 3]  

我想出的代码是这样的,但是它返回一个空数组

#ifndef STRUCT_STRING_ARRAY
#define STRUCT_STRING_ARRAY
typedef struct s_string_array
{
    int size;
    char** array;
} string_array;
#endif

#ifndef STRUCT_INTEGER_ARRAY
#define STRUCT_INTEGER_ARRAY
typedef struct s_integer_array
{
    int size;
    int* array;
} integer_array;
#endif

integer_array* my_count_on_it(string_array* str){
    integer_array* intArr;
    int size = str->size,  count =0;
    intArr= malloc(1);  
    intArr[0].array = malloc(size*sizeof(intArr));  
    for(int i = 0 ; i < size; i++){
        for(int j =0 ; str->array[i][j] != '\0';j++) count++;
        intArr->array[i] = count;
        count =0;
    }
    return intArr;
}

注意我不能改变结构,我只需要实现上面的这个功能

【问题讨论】:

  • intArr[0].array = malloc(size*sizeof(intArr)); 不应该是sizeof(int)吗?
  • 阅读malloc 的手册。特别是考虑到 Paul 的评论和您的 malloc(1) 电话。
  • 如何测试数组是否为空?是否可能与您没有设置intArr-&gt;size这一事实有关?
  • 非常感谢我找到解决方案的提示。

标签: c struct dynamic-memory-allocation c-strings function-definition


【解决方案1】:

malloc调用的参数

intArr= malloc(1); 

不正确。只分配了一个字节而不是sizeof( integer_array ) 字节。

再次声明

intArr[0].array = malloc(size*sizeof(intArr));

malloc 调用的参数不正确。你需要写

intArr[0].array = malloc( size * sizeof( int ) );

另外你忘了设置对象intArr的数据成员size

而不是这个手动编写的循环

for(int j =0 ; str->array[i][j] != '\0';j++) count++

您可以使用标准 C 函数 strlen

对于初学者来说,结构应该被声明为

typedef struct s_string_array
{
    size size;
    char **array;
} string_array;

typedef struct s_integer_array
{
    size_t size;
    size_t *array;
} integer_array;

在函数中你应该检查内存是否分配成功。

该函数可以如下面的演示程序所示。

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

typedef struct s_string_array
{
    size_t size;
    char **array;
} string_array;

typedef struct s_integer_array
{
    size_t size;
    size_t *array;
} integer_array;

integer_array * my_count_on_it( const string_array *str )
{
    integer_array *intArr = malloc( sizeof( *intArr ) );
    int success = intArr != NULL;
    
    if ( success )
    {
        intArr->array = malloc( str->size * sizeof( *intArr->array ) );
        success = intArr->array != NULL;
        
        if ( success )
        {
            intArr->size = str->size;
            for ( size_t i = 0; i < str->size; i++ )
            {
                intArr->array[i] = strlen( str->array[i] );
            }
        }
        else
        {
            free( intArr );
            intArr = NULL;
        }
    }
    
    return intArr;
}    

int main(void) 
{
    char * s[] = { "This", "is", "the", "way" };
    string_array str = { sizeof( s ) / sizeof( *s ), s };
    
    integer_array *intArr = my_count_on_it( &str );
    
    if ( intArr != NULL )
    {
        for ( size_t i = 0; i < intArr->size; i++ )
        {
            printf( "%zu ", intArr->array[i] );
        }
        
        putchar( '\n' );
        
        free( intArr->array );
        free( intArr );
    }
    
    return 0;
}

程序输出是

4 2 3 3 

【讨论】:

    【解决方案2】:

    我必须在结构中添加数组大小。

    所以新功能是

    integer_array* my_count_on_it(string_array* str){
    integer_array* intArr;
    int size = str->size,  count =0;
    intArr= malloc(1);  
    intArr->size = size;
    intArr->array = malloc(intArr->size*sizeof(intArr->size));  
    for(int i = 0 ; i < size; i++){
        for(int j =0 ; str->array[i][j] != '\0';j++) count++;
        intArr->array[i] = count;
        count =0;
    }
    return intArr; }
    

    【讨论】:

    • malloc(1)(要求 1 个字节)分配仍然是错误的。另一个也是。它可能不会立即让你崩溃,但它有可能。
    • 您能否更具体地说明哪一个?
    • malloc(1) 是错误的,因为结构不是 1 字节长,所以使用 malloc (sizeof(integer_array)) 代替,另一个 malloc(intArr-&gt;size*sizeof(intArr-&gt;size)) 是巧合正确,但它应该是 malloc(intArr-&gt;size*sizeof(int))因为它是一个整数数组,而不是大小(如果有人更改大小的类型,你的数组会损坏......)
    • 非常感谢您的提示,根据您的建议进行修改后似乎可以正常工作。我今天学到了很多关于 malloc 的知识:)
    猜你喜欢
    • 2021-09-22
    • 2020-07-16
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多