如何分配结构数组 [英] How to assign an array of structs

查看:78
本文介绍了如何分配结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为create()的函数,该函数返回指向名为ann的结构的指针,如下所示

  typedef struct ann {
个int输入; / *输入神经元的数量* /
int hidden_​​layers; / *隐藏层数* /
int隐藏; / *隐藏的神经元数量* /
int输出; / *输出神经元的数量。 * /
int权重; / *总nof weigths(染色体)* /
int个神经元; / *神经元总数* /
双*权重; / *权重(基因型)* /
double *输出; / *输出* /
双重适应度; / *网络的总体适应度* /
double * delta;
actfun activation_hidden; / *隐藏层激活函数* /
actfun activation_output; / *输出层激活函数* /
} ann;

函数create()的原型

  ann * create(int输入,int hidden_​​layers,int隐藏,int输出); 

我需要一个an s数组,所以我有以下内容

  int人口数= 10; 
ann *人口= malloc(人口大小* sizeof(ann));

for(i = 0; i<人口规模; i ++){
人口[i] =创建(trainset-> num_inputs,1,隐藏,trainset-> num_outputs);
}

但我遇到以下错误

 错误:从类型'ann * {aka struct ann *}'分配类型为'ann {aka struct ann}'时,类型不兼容

我的问题是如何对人口中的当前元素进行类型转换,以便将返回的struct(指针)ann可以存储在人口中。 / p>

此处要求的是函数create()的完整代码

  ann * create(int输入,int hidden_​​layers,int隐藏,int输出){

如果(hidden_​​layers< 0)返回0;
如果(输入< 1)返回0;
如果(输出< 1)返回0;
如果(hidden_​​layers> 0&& hidden< 1)返回0;


const int hidden_​​weights = hidden_​​layers吗? (输入+1)*隐藏+(hidden_​​layers-1)*(隐藏+1)*隐藏:0;
const int output_weights =(hidden_​​layers?(hidden + 1):(inputs + 1))*输出;
const int total_weights =(hidden_​​weights + output_weights);

const int total_neurons =(输入+隐藏* hidden_​​layers +输出);

/ *为权重,输出和增量分配额外的大小。 * /
const int size = sizeof(ann)+ sizeof(double)*(total_weights + total_neurons +(total_neurons-输入));
ann * ret = malloc(size);
如果(!ret)返回0;

ret-> inputs =输入;
ret-> hidden_​​layers = hidden_​​layers;
ret-> hidden =隐藏;
ret->输出=输出;

ret-> weights = total_weights;
ret-> neurons = total_neurons;

/ *设置指针。 * /
ret-> weight =(double *)((char *)ret + sizeof(ann));
ret->输出= ret->权重+ ret->权重;
ret->δ= ret->输出+ ret->神经元;

ann_randomize(ret);

ret-> activation_hidden = ann_act_sigmoid_cached;
ret-> activation_output = ann_act_sigmoid_cached;

ann_init_sigmoid_lookup(ret);

回程;
}


解决方案

错误

 错误:从类型'ann * {aka struct ann *}分配类型为'ann {aka struct ann}'时类型不兼容b  

出现是因为人口[i] struct ann ,而不是指向它的指针。不同类型!



正如斯蒂芬·莱希纳(Stephan Lechner)的回答所指出的,您需要做的是更改外部数组(使其成为指针数组)或返回的类型。 create()函数,使其返回结构本身。



我将建议您的内容是对 create()接口的更改,以便将指向它的指针作为参数传递给要填充的输出结构。



在调用方函数中定义了指向 ann 的指针数组:

  ann *人口[10] = {0}; 

for(i = 0; i< 10; i ++){
if(create(& population [i],trainset-> num_inputs,1,隐藏,trainset-> ; num_outputs)< 0){
printf(在创建ann#%d\n期间发出的问题,i);
休息时间;
}
}

//记住释放指针!

没有分配!它在 create()内部移动,这将有两个界面更改:


  1. 输出指针作为参数传递(在我的示例中为第一个)

  2. 返回错误代码(成功时为0,失败时为<0)



  int create(ann ** outstruct,int输入,int hidden_​​layers,int隐藏,int输出){
int ret = 0;

ann * tmp = malloc(sizeof(ann));

// ...
//进行初始化新分配的结构所需的任何操作

//如果出现问题,请将ret设置为负值
// ... ...但是在这种情况下,请在返回前释放tmp!
// ...

outstruct = tmp;

回程;
}

在我的示例中为 ann ** 指针必须传递给 create(),因为它在内部分配了内存。



简单如果结构是从外部分配的,并且 create()仅具有填充它的作用,则指针就足够了。


i have a function called create() that return a pointer to a struct named ann as shown below

typedef struct ann {
    int inputs;                 /* Number of input neurones      */
    int hidden_layers;          /* Number of hidden layers       */
    int hidden;                 /* Number of hidden neurones     */
    int outputs;                /* Number of output neurons.     */
    int weights;                /* Total nof weigths(chromosomes)*/
    int neurons;                /* Total Number of neurones      */
    double *weight;             /* The weights(genotype)         */
    double *output;             /* Output                        */
    double fitness;              /* Total fitness of the network    */
    double *delta;
    actfun activation_hidden;   /* Hidden layer activation func  */
    actfun activation_output;   /* Output layer activation func  */
} ann;

prototype of the function create()

ann *create(int inputs, int hidden_layers, int hidden, int outputs);

i need an array of ann s, so i have the following

int population_size = 10;
ann *population = malloc ( population_size * sizeof(ann));

    for( i = 0; i < population_size; i++ ){
        population[i] = create( trainset->num_inputs, 1 , hidden, trainset->num_outputs);
    }

but i am getting the following error

error: incompatible types when assigning to type ‘ann {aka struct ann}’ from type ‘ann * {aka struct ann *}’

My Question is how to type cast the current element in population so that the returned struct (a pointer) ann can be stored in population

As requested here is the full code of the function create()

ann *create   ( int inputs, int hidden_layers, int hidden, int outputs ) {

    if (hidden_layers < 0) return 0;
    if (inputs < 1) return 0;
    if (outputs < 1) return 0;
    if (hidden_layers > 0 && hidden < 1) return 0;


    const int hidden_weights = hidden_layers ? (inputs+1) * hidden + (hidden_layers-1) * (hidden+1) * hidden : 0;
    const int output_weights = (hidden_layers ? (hidden+1) : (inputs+1)) * outputs;
    const int total_weights = (hidden_weights + output_weights);

    const int total_neurons = (inputs + hidden * hidden_layers + outputs);

    /* Allocate extra size for weights, outputs, and deltas. */
    const int size = sizeof(ann) + sizeof(double) * (total_weights + total_neurons + (total_neurons - inputs));
    ann *ret = malloc(size);
    if (!ret) return 0;

    ret->inputs = inputs;
    ret->hidden_layers = hidden_layers;
    ret->hidden = hidden;
    ret->outputs = outputs;

    ret->weights = total_weights;
    ret->neurons = total_neurons;

    /* Set pointers. */
    ret->weight = (double*)((char*)ret + sizeof(ann));
    ret->output = ret->weight + ret->weights;
    ret->delta = ret->output + ret->neurons;

    ann_randomize(ret);

    ret->activation_hidden = ann_act_sigmoid_cached;
    ret->activation_output = ann_act_sigmoid_cached;

    ann_init_sigmoid_lookup(ret);

    return ret;
}

解决方案

The error

error: incompatible types when assigning to type ‘ann {aka struct ann}’ from type ‘ann * {aka struct ann *}

occurs because population[i] is a struct ann, not a pointer to it. Different types!

What you need to do, as already stated by Stephan Lechner's answer, is either change the array outside (making it an array of pointers) or the returned type of the create () function, making it return the structure itself.

What I'm going to suggest you is a change of create() interface in order to pass a pointer to it as a parameter the output structure to be populated.

In the caller function where the array of pointers to ann is defined:

ann *population[10] = { 0 };

for( i = 0; i < 10; i++ ){
    if ( create( &population[i], trainset->num_inputs, 1 , hidden, trainset->num_outputs) < 0 ){
        printf ("Issues during creation of ann #%d\n", i);
        break;
    }
} 

// Remember to free the pointers!

No allocation! It is moved inside create(), that will have two interface changes:

  1. The output pointer is passed as a parameter (the first, in my example)
  2. Returns an error code (0 on success, < 0 on failure)

int create(ann **outstruct, int inputs, int hidden_layers, int hidden, int outputs){
    int ret = 0;

    ann *tmp = malloc( sizeof(ann));

    // ...
    //do whatever you need to initialize the newly allocated struct

    // set ret to a negative value if anything goes wrong
    // ... but in this case free tmp before returning!
    //...

    outstruct = tmp;

    return ret;
}

In my example an ann ** pointer had to be passed to create() because it internally allocated the memory.

A simple pointer is enough if the structs are allocated externally and create() has just the role to populate it.

这篇关于如何分配结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆