【问题标题】:cc compiler issue in CC中的cc编译器问题
【发布时间】:2014-02-22 17:05:33
【问题描述】:

我正在尝试编写一个简单的代码来用 C 语言构造一棵树。下面是我的代码 sn-p。

#include<stdio.h>

struct node
{
  int data;
  struct node *left;
  struct node *right;
};

int main()
{
  struct node *root = newNode(5);
  //struct node *root = NULL; working piece
  //newNode(&root,5); working piece
  if(root == NULL)
  {
    printf("No root\n");
    return 0;
  }
  //root->left = newNode(4);
  //root->right = newNode(3);
  //root->left->left = newNode(2);
  //root->right->right = newNode(1);

  return 0;
}

struct node* newNode(int data)
{
  struct node *temp;
  temp = (struct node*) malloc(sizeof(struct node));
  temp->data = data;
  temp->left = NULL;
  temp->right = NULL;

  return(temp);
}

当我尝试返回结构节点地址时,编译器给了我错误

"rightNode.c", line 29: identifier redeclared: newNode
        current : function(int) returning pointer to struct node {int data, pointer to struct node {..} left, pointer to struct node {..} right}
        previous: function() returning int : "rightNode.c", line 12

但是当我评论这个 struct node* newNode(int data) 并尝试定义一个返回 int 的函数时,将结构的地址传递给下面的函数,它没有显示任何错误。

int newNode(struct node **root,int data)
{
  printf("Inside New Node\n");
  return 0;
}

据我所知,在 C 中将结构体的地址返回给调用函数是合法的。

这与编译器有关。

我在unix环境下使用cc编译器

type cc
cc is a tracked alias for /apps/pcfn/pkgs/studio10/SUNWspro/bin/cc

下面是我用来编译cc rightNode.c的命令

任何帮助将不胜感激......

【问题讨论】:

  • @self-谢谢它没有显示任何错误。但我的疑问是,有必要声明函数的原型吗?如果是这样,为什么在返回 int 时它没有显示任何错误
  • 原型,还包括stdlib.h for malloc
  • @arunb2w 编译器会猜测如果函数无法“找到”它,它会返回一个 int。
  • @self - 如果我将 main 函数的返回类型更改为 void,编译器会在不指定原型的情况下猜测被调用函数的返回类型为 void。
  • @arunb2w:C标准明确规定main的返回类型必须是int。如果类型不是int,则您的程序具有未定义的行为,这意味着它可以工作,但不能保证它会工作。例如,我使用的编译器说 main 返回 void 是错误的。

标签: c unix cc


【解决方案1】:

将此struct node* newNode(int data) 放在代码上方并包含stdlib.h

如果要在声明函数之前使用函数,则需要函数原型。 malloc 也在 stdlib.h 中定义。

【讨论】:

    【解决方案2】:

    你需要在使用之前声明一个newNode原型。

    // somewhere after struct node definition and before first use
    struct node* newNode(int);
    

    您还需要包含stdlib.h 才能获得malloc

    【讨论】:

    • 谢谢它没有显示任何错误。但我的疑问是,有必要声明函数的原型吗?如果是这样,为什么在返回 int 时它没有显示任何错误
    • @arunb2w 有一个非常古老的遗留功能叫做:implicit function declarations。如果您在C99 模式下编译代码,则会出现错误。为您的编译器尝试-std=c99-std=c11 之类的参数,然后查看。
    【解决方案3】:

    调用struct node *root = newNode(5);时看不到函数原型,因此编译器会感到困惑。

    【讨论】:

      【解决方案4】:

      当编译器找不到函数声明时,它假定该函数存在,但返回int。在main 中调用newNode(...) 之前声明struct node* newNode(int data);

      【讨论】:

        【解决方案5】:

        在旧版本的 C 中,您不需要在使用函数之前声明它。在较旧的 C 中,假定未声明的函数返回 int 并接受未指定数量的参数。这就是您收到错误的原因,因为编译器假定newNode 函数返回int,而不是struct node *

        在现代 C(C99 和更高版本)中,您不能再这样做了。您必须在使用函数之前声明它们。一些编译器仍然允许旧的行为并警告它,但是一个严格符合 C99 的程序不能使用一个函数而不先声明它。

        在您的情况下,您应该将以下代码行放在您的 main 函数之前。这告诉编译器newNode 函数以及它应该如何被调用:

        struct node *newNode(int);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-18
          • 2020-01-22
          • 2019-11-27
          • 2010-12-06
          • 2017-05-21
          相关资源
          最近更新 更多