0

I'm working on a C assignment for school and the assignment asks us to use this specific function signature that is causing errors for my compiler.

I'm getting an error from line 38 (the smallest function signature) in vector_test.c, the error reads ""," expected (got "*")". This is my first time working in C so I think I must be doing something wrong in regards to how I have setup the typedef in types.h or something along those lines, just not exactly sure and thought I'd get some extra opinions. Anything you could point out that I'm doing wrong here would be helpful, thank you!

Here's the code:

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

Here's the assignment instructions for this specific part so you can see what I'm trying to do:

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
Nyoxide
  • 1
  • 1
  • 1
    You don't have a type called `Vector3t`. You have`struct vec3`, `struct Vector3t`, and your typedef of the latter is `vec3`. You can't just use the name of the struct type without the `struct` in front. – Dmitri Feb 12 '17 at 04:17
  • You got the first point of your assignment wrong. Thus the second part is invalid. – DeiDei Feb 12 '17 at 04:19
  • Changing my typedef code to look like this seemed to do the trick: typedef struct vec3 Vector3t; Thanks for your help! – Nyoxide Feb 12 '17 at 04:32

2 Answers2

0

Your function forward declaration is:

int smallest(const Vector3t, int);

While your function definition says:

int smallest(const Vector3t* pts, int n)

In your forward declaration you're saying that you're passing in the struct as a parameter, while in your definition you're saying that it's taking a pointer to the struct. These are incompatible signatures.

Caleb
  • 1,113
  • 5
  • 13
  • 3
    He doesn't have a type called `Vector3t` anyway. – Dmitri Feb 12 '17 at 04:23
  • True. There are a few errors in this code, but I believe that's the cause of the particular compiler complaint he was asking about. – Caleb Feb 12 '17 at 04:24
  • I'd say his error is caused by the compiler trying to treat his (nonexistent) type name as the name of the parameter instead of its type, then encountering gibberish instead of the comma separating the parameters... – Dmitri Feb 12 '17 at 04:28
0

You get it wrong in the first steps:

  1. Create a header file called "types.h" (with a macro guard)

    In this header file, create a struct type called vec3 with int types: x, y, and z

    Create a typedef to the struct vec3 above and call it Vector3t

For first,

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

is correct. Then to define the typedef, you first give the the actual type, then the type alias:

typedef struct vec3 Vector3t;

Alternatively, these 2 can be combined into one typedef:

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

Also the declaration of smallest doesn't match the definition (shouldn't they look alike?) and the return type of main must be int.

Community
  • 1
  • 1
Antti Haapala
  • 117,318
  • 21
  • 243
  • 279