C参考函数传递复杂结构

时间:2015-04-30 06:07:13

标签: c data-structures cuda compiler-errors

我复制了here提供的指导,但是对于每一行都有一个函数头和原型继续收到以下错误:'typedef“Neuron”可能不会用在精心设计的类型说明符中。我研究了这个错误并没有找到太多有用的内容。任何帮助都将非常感激。

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

struct neuronHead {
  int x, y, z;                    
  neuronHead *apical[25];  
  neuronHead *basal[25];    
  neuronHead *axon[25];      
  int time;                       
} neuron;

typedef struct neuron Neuron;

void setupBrain(struct Neuron ****brain); /* brain is a 3D array of structs */
void freeBrain(struct Neuron ****brain);

int main(void) {
   Neuron ***brain;

   setupBrain(&brain);
   freeBrain(&brain); }

void setupBrain(struct Neuron ****brain) {
   /* code for malloc'ing a 3D array of structures */ }

void freeBrain(struct Neuron ****brain) {
   /* code for freeing the 3D array of structures */ }

我正在运行Ubuntu 14.04并使用Nvidia的NVCC编译器在GPU上运行代码,尽管这与手头的错误无关。

3 个答案:

答案 0 :(得分:0)

在您的代码中,您可以键入def struct neuron。在您的情况下,neuronstruct neuronHead类型的全局变量。

您需要将代码更改为

struct neuronHead {
  int x, y, z;                    
  struct neuronHead *apical[25];  
  struct neuronHead *basal[25];    
  struct neuronHead *axon[25];      
  int time;                       
};

typedef struct neuronHead Neuron;

或,

typedef struct neuronHead Neuron;

struct neuronHead {
  int x, y, z;                    
  Neuron *apical[25];  
  Neuron *basal[25];    
  Neuron *axon[25];      
  int time;                       
};

,或者

  typedef struct neuronHead {
  int x, y, z;                    
  struct neuronHead *apical[25];  
  struct neuronHead *basal[25];    
  struct neuronHead *axon[25];      
  int time;                       
} Neuron;

答案 1 :(得分:0)

考虑创建int的实例:

int some_int;

C中,通常语法为:

<type> <variable_name>

当你写完这样的东西时:

struct Thing {
    int i;
    /* Other members... */
} thing;

这实际上意味着:创建一个名为thing的变量,其类型为struct Thing,此结构定义如下:{ int i; /* Other members... */ }

然后你做这样的事情:

typedef struct thing SomeThing;

此时,没有意义,因为您正在尝试typedef一个实例(thing)。

你可能意味着这样的事情:

struct Thing {
    int i;
    /* Other members... */
}; /* Nothing here! It's the end of type definition. */

typedef struct Thing thing; /* Typedef "struct Thing" -> "thing". */

typedef的一般语法是:

typedef <original_type> <new_alias>

所以它可以缩短:

typedef /* Typedef the following struct... */
    struct Thing {
        int i;
        /* Other members... */
    }
thing; /* ... to such a name. */

然后您可以正确使用thing作为变量类型。

同样C中没有引用,只有指针。

答案 2 :(得分:0)

你应该替换

typedef struct neuron Neuron;

typedef struct neuronHead Neuron;

写作时

struct neuronHead { ... } neuron;

你介绍了两个要素:

  • 名为“struct neuronHead”的复合数据类型,

  • 和一个名为“neuron”的变量,其类型为“struct neuronHead”。

Typedef允许为数据类型创建别名

typedef <original_type> <alias_type>;

虽然在您的示例中是&lt; original_type&gt; (struct neuron)不是一个合适的类型。适当的类型定义都是:

struct neuronHead { ... } neuron; /* data type AND variable were introduced */
typedef struct neuronHead Neuron; /* data type introduced */

typedef struct neuronHead { ... } Neuron; /* data type AND type alias were introduced */

预定义类型的示例用法:

struct neuronHead n1; /* correct */
Neuron            n2; /* also correct */
struct neuron     n3; /* incorrect: 'neuron' is variable name! */

谢谢。

相关问题