2017-02-12 64 views
0

我工作的一个C赋值为学校和作业要求我们使用导致的错误我的编译器这个特定的函数签名。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 
+1

你没有一个叫做'Vector3t'类型。你有'结构vec3','struct Vector3t',后者的typedef是'vec3'。你不能只使用前面没有'struct'的struct类型的名称。 – Dmitri

+0

你的任务第一点错了。因此第二部分是无效的。 – DeiDei

+0

改变我的typedef代码看起来像这样做:typedef struct vec3 Vector3t; 感谢您的帮助! – Nyoxide

回答

0

你的函数正声明:

int smallest(const Vector3t, int);

当你的函数定义说:

int smallest(const Vector3t* pts, int n)

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

+3

反正他没有一个叫做Vector3t的类型。 – Dmitri

+0

是的。这段代码有一些错误,但我相信这是他所问的特定编译器投诉的原因。 – Caleb

+0

我想说他的错误是由编译器试图将他(不存在)类型的名称作为参数的名称而不是其类型,然后遇到乱码而不是逗号分隔参数引起的... – Dmitri

0

你弄错了第一步骤:

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

    在这个头文件,创建一个名为vec3与结构类型int类型:xy,并且z

    创建typedef到上方和struct vec3称之为Vector3t

对于第一,

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

是正确的。然后,以限定typedef,首先得到的实际类型,则该类型别名:

typedef struct vec3 Vector3t; 

备选地,这些2可以组合成一个的typedef:

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

另外声明的smallest不匹配的定义(不应该他们长得很像?)和the return type of main must be int