【问题标题】:New to programming, issue with structures and functions编程新手,结构和功能问题
【发布时间】:2015-01-28 08:39:53
【问题描述】:

嘿,我是编程新手(通过 C 语言中的 cs50x 学习),当他们提到结构时,我决定试着绕开,只是编写一个快速程序,使用函数交换结构中的一些值。我一直在运行,直到出现几条错误消息,其中第一条是“不兼容的指针类型将'struct numbers*'传递给'struct numbers*'类型的参数。另一个问题似乎出现在函数定义中,编译器说“不完整“结构编号”类型的定义”我只是希望能得到一些帮助,因为我很难过。

这是代码(我知道它很粗糙,但我正在学习,哈哈)

#include <stdio.h>
struct numbers;
void swap(struct numbers* s);

int main(void)
{
    struct numbers
    {
        int a;
        int b;
        int c;
    };
    struct numbers x = {1, 5 , 9};
    swap(&x);
    printf("%i, %i, %i\n", x.a, x.b, x.c);
    return 0;
}

void swap(struct numbers* s)
{
    int temp = s -> a;
    int temp2 = s -> b;
    s -> a = s -> c;
    s -> b = temp;
    s -> c = temp2;
}

【问题讨论】:

  • “main 内部定义的结构编号”和“任何函数外部的结构编号”被认为是不同的类型。将定义移到main之外。
  • 欢迎来到 SO。虽然问题通常没问题,但如果下次发布问题时指定哪个行引发每个错误,每个人都会更容易。不要忘记将解决您的问题的答案之一(如果有的话)标记为解决方案。
  • 如前所述,将struct 定义设为全局。此外,在处理不同事物的行之间放置额外的空间(例如在您的#include 之后,C 不关心缩进。您可能还想在printf 中使用%d,它更常见。跨度>

标签: c


【解决方案1】:

您希望swap() 中的代码能够访问struct numbers 的字段,但该类型的完整声明 main() 中,因此它不可见。

分解声明,它必须对所有需要它的人可见。将其放在首位也将无需预先声明结构。

swap() 本身也是如此,将其放在 main() 之前将无需在同一文件中为其创建原型。

应该是:

struct numbers
{
 .
 .
 .
}

static void swap(struct numbers *s)
{
 .
 .
 .
}

int main(void)
{
 .
 .
 .
}

【讨论】:

  • 谢谢,解决了这个问题。
  • 可能还值得使用typedef,以便可以创建结构的多个实例:typedef struct { int a; int b; int c; } NUMBERS; 允许您以与使用@987654330 相同的方式简单地编写static void swap(NUMBERS *s) @ 或 float*.
  • 当然,这很方便,但我不明白您所说的“多个实例”是什么意思。这与typedef 无关,在 OP 的代码中也不是问题。请注意main() 中以struct numbers x = 开头的行,它正在创建一个实例,并且可以有任意数量的实例。
  • @unwind 嗯,我好像打错了;我的意思是说“如果要创建结构的多个实例,为了方便,也可能值得使用typedef”。 (这并不是要批评您的回答或任何内容 - 我只是认为不值得单独发布。)
【解决方案2】:

问题在于struct numbers声明是全局的,而定义main是局部的,要使用结构的成员,@987654323 @function 必须知道结构有哪些成员,因为它看不到定义,所以它不知道。删除声明并将定义放在全局范围内。

【讨论】:

    【解决方案3】:

    函数swap 看不到struct numbers 的定义。将其全局放在main之外。

    额外提示 - 将typedef 与结构一起使用,它使您可以灵活地声明:

    typedef struct typeNumbers
    {
        int a;
        int b;
        int c;
    } numbers;
    

    请注意,typeNumbers 是可选的。声明如下:

    numbers x = {1, 2, 3};
    

    【讨论】:

      【解决方案4】:

      问题是结构是主要的,我也对代码做了一些修复并注释它们。

      #include <stdio.h>
      
      //By defining the struct at the beginning you can avoid the forward declaration 
      //and it make more sense to know what "numbers" is before continuing reading the code.
      struct numbers {
          int a;
          int b;
          int c;
      };
      
      void swap(struct numbers* s)
      {
          //Small change to use only one temp variable...
          int temp2 = s -> b;
          s -> b = s -> a;
          s -> a = s -> c;
          s -> c = temp2;
      }
      
      int main(void)
      {
          struct numbers x = {1, 5 , 9};
          swap(&x);
          printf("%i, %i, %i\n", x.a, x.b, x.c);
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2015-11-17
        • 1970-01-01
        • 2020-01-28
        • 1970-01-01
        • 2018-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多