2017-02-12 109 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