【发布时间】:2017-02-11 05:52:24
【问题描述】:
我收到以下代码的编译错误。我已经尽力了,但无法弄清楚。任何帮助将不胜感激。
#include <stdio.h>
#include <stdlib.h>
#define N 100
int counter=0;
struct node {
int value; };
struct node *p = (struct node *) malloc (N*sizeof (node));
void main()
{
int a = 5, b=6;
struct node * c = 0;
c = add(a,b);
}
void add(int m, int n)
{
struct node * pin_1;
struct node * pin_2;
struct node * pin_0;
pin_0->value = m;
pin_1->value = n;
pin_2->value = m + n;
counter++;
printf("value of out is %d /n", pin_2->value);
}
我在 GCC 中遇到错误:
struct_check.c:9:错误:‘node’此处未声明(不在函数中)
【问题讨论】:
-
c = add(a,b);:add返回类型为void。pin_1、pin_2和pin_0未初始化。 (不需要使用指针。) -
并在函数内移动 malloc。
-
或
struct node *p = (struct node *) malloc (N*sizeof (node));-->struct node p[N];
标签: c struct compiler-errors dynamic-memory-allocation