C函数签名问题

标签 c function pointers typedef declaration

我正在为学校做一项 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;
}

类型.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

最佳答案

您的函数前向声明是:

int 最小(const Vector3t, int);

虽然你的函数定义说:

int 最小(const Vector3t* pts, int n)

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

关于C函数签名问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42184135/

相关文章:

socket.io 的 C 客户端

c - 如何使用 jsmn 构建 C 脚本?

c - ANTLR4 C 语法不支持 __cdecl?

javascript - 如何创建 JavaScript 延迟函数

function - 运算符的 Haskell 类型优先级高于函数

c++ - 删除其引用在三个不同列表中维护的指针对象

c - 转换为 void 指针时,表达式必须是指向完整对象类型的指针

r - 在 R 中使用 "~ call"和动态变量

c++ - 指针函数

c++ - 无法从指针访问映射成员