【问题标题】:C function signature problemsC函数签名问题
【发布时间】:2017-02-12 04:12:50
【问题描述】:

我正在为学校做 C 作业,作业要求我们使用这个导致编译器出错的特定函数签名。

我在 vector_test.c 中的第 38 行(最小的函数签名)收到一个错误,错误显示为“”、“预期(得到“*”)”。这是我第一次在 C 中工作,所以我认为我在 types.h 中设置 typedef 或类似的东西方面一定做错了,只是不太确定,我想我会得到一些额外的意见。您可以指出我在这里做错的任何事情都会有所帮助,谢谢!

代码如下:

vector_test.c

#include "types.h"

int smallest(const Vector3t, int);

void main (int argc, char *argv[]) {
    unsigned int N = 0;

    printf ("Number of elements ===>");
    scanf ("%u", &N);
    struct Vector3t points[N];

    for (int i = 0; i < N; i++)
    {
        scanf("%d,%d,%d", &points[i].x, &points[i].y, &points[i].z);
    }

    for (int p = 0; p < N; p++)
    {
        printf("%i", points[p].x);
        printf("%i", points[p].y);
        printf("%i\n\n", points[p].z);
    }

    int result = smallest(points, N);

    printf("%s", "The point closest to the origin is (");
    printf("%i", points[result].x);
    printf("%s", ", ");
    printf("%i", points[result].y);
    printf("%s", ", ");
    printf("%i", points[result].z);
    printf("%s", ")");
}

int smallest(const Vector3t* pts, int n)
{
    int shortest = 99999999;
    int shortIndex = -1;

    for (int i = 0; i < n; i++)
    {
        int distance = pts[i].x + pts[i].y + pts[i].z;
        if (distance < shortest)
        {
            shortest = distance;
            shortIndex = i;
        }
    }

    return shortIndex;
}

types.h

#ifndef types_H
#define types_H

struct vec3 {
   int x;
   int y;
   int z;
};

typedef struct Vector3t {
   int x;
   int y;
   int z;
} vec3;

#endif

这是此特定部分的作业说明,因此您可以看到我正在尝试做什么:

1. Create a header file called “types.h” (with a macro guard)
   a. In this header file, create a struct type called vec3 with int types: x, y, and z
   b. Create a typedef to the struct vec3 above and call it Vector3t
2. Create a C source file called “vector_test.c”
   a. This file should include “types.h” and any other C system includes you may need
   b. Create a main function that does the following:
      i. Prompts the user “Number of elements ===> “
      ii. Read an unsigned int from the user – this variable will be referred to as N
      iii. Create an array of Vector3t of size N and call it points
      iv. Read in triples of int (comma separated) from the user to populate the entire array
      v. Using the function signature: int smallest(const Vector3t* pts, int n), implement a
         function that returns the index of the point that is the closest to the point (0, 0, 0)
      vi. Call this function and store the index into an int called result
      vii. Print out to the user “The point closest to the origin is (<x>, <y>, <z>)” where <x>, <y>, and <z>
           correspond to the values of points[result]
      viii. Free all allocated data

【问题讨论】:

  • 您没有名为 Vector3t 的类型。你有struct vec3struct Vector3t,你的typedef是vec3。前面没有struct,你不能只使用结构类型的名称。
  • 你的作业的第一点错了。因此第二部分无效。
  • 改变我的 typedef 代码看起来像这样似乎可以解决问题: typedef struct vec3 Vector3t;感谢您的帮助!

标签: c function pointers typedef declaration


【解决方案1】:

你的函数前向声明是:

int smallest(const Vector3t, int);

虽然你的函数定义说:

int smallest(const Vector3t* pts, int n)

在您的前向声明中,您说您将结构作为参数传递,而在您的定义中,您说它采用指向结构的指针。这些是不兼容的签名。

【讨论】:

  • 反正他没有一个叫做Vector3t的类型。
  • 是的。这段代码有一些错误,但我相信这是他询问的特定编译器投诉的原因。
  • 我会说他的错误是由于编译器试图将他的(不存在的)类型名称视为参数的名称而不是其类型,然后遇到乱码而不是逗号分隔参数。 ..
【解决方案2】:

你在第一步就搞错了:

  1. 创建一个名为 "types.h" 的头文件(带有宏保护)

    在此头文件中,创建一个名为 vec3 的结构类型,int 类型:xyz

    为上面的struct vec3 创建一个typedef 并命名为Vector3t

首先,

struct vec3 {
   int x;
   int y;
   int z;
};

是正确的。然后定义typedef,首先给出实际类型,然后是类型别名:

typedef struct vec3 Vector3t;

或者,这两个可以组合成一个typedef:

typedef struct vec3 {
   int x;
   int y;
   int z;
} Vector3t;

smallest 的声明也与定义不匹配(它们不应该看起来相似吗?)和the return type of main must be int

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-11
    • 2011-06-21
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 2011-01-16
    相关资源
    最近更新 更多