C函数签名问题

时间:2017-02-12 04:12:50

标签: 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;
}

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

2 个答案:

答案 0 :(得分:0)

您的功能转发声明是:

int smallest(const Vector3t, int);

虽然你的功能定义是:

int smallest(const Vector3t* pts, int n)

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

答案 1 :(得分:0)

你在第一步中弄错了:

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

         

    在此头文件中,创建一个名为vec3的结构类型,其类型为intxyz

         

    为上面的typedef创建struct vec3并将其称为Vector3t

  2.   

首先,

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