简体   繁体   English

从结构 c 语言返回数组

[英]Return the array from a struct c language

I'm trying to return the array inside the struct.我正在尝试返回结构内的数组。

I have to make a new array inside the struct integer_array and then return the array at the end.我必须在 struct integer_array 中创建一个新数组,然后在最后返回该数组。

The assignment is to take a string array and count each index and how what is the size of it.任务是获取一个字符串数组并计算每个索引以及它的大小。

So an array that is所以一个数组是

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

should return:应该返回:

[4, 2, 3, 3]  

The code I came up with is this, but it returns an empty array我想出的代码是这样的,但它返回一个空数组

#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; }

NOTE I can't change the structures, and I have only to implement this function above注意我不能改变结构,我只需要实现上面的这个 function

The argument of the call of malloc malloc调用的参数

intArr= malloc(1); 

is incorrect.是不正确的。 There is allocated only one byte instead of sizeof( integer_array ) bytes.只分配了一个字节而不是sizeof( integer_array )字节。

Again in this statement再次在此声明中

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

the argument of the call of malloc is incorrect. malloc 的调用参数不正确。 You need to write你需要写

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

Also you forgot to set the data member size of the object intArr .此外,您忘记设置 object intArr的数据成员size

Instead of this manually written loop而不是这个手动编写的循环

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

you could use the standard C function strlen .您可以使用标准 C function strlen

For starters the structures should be declared like对于初学者来说,结构应该被声明为

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

and

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

Within the function you should check whether a memory was allocated successfully.在 function 中,您应该检查 memory 是否分配成功。

The function can look the following way as it is shown in the demonstrative program below. function 可以如下面的演示程序所示。

#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;
}

The program output is程序 output 是

4 2 3 3 

I had to add the array size in the struct.我必须在结构中添加数组大小。

So the new function would be所以新的 function 将是

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; }

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

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