Reputation: 41
I tried many times to fix the error "Incomplete Definition of Type of struct *
,please can you help me to find the problem and fix it ?
Any advice on this?
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
struct mypoint;
typedef struct mypoint {
int x;
int y;
};
void setRandomPoint(struct mypoint *a, int min, int max)
{
do {
a->x = rand();
} while (a->x > max || a->x < min);
do
{
a->y = rand();
} while (a->y > max || a->y < min);
}
int main()
{
struct myPoint point;
int min = 0, max = 0;
int i;
srand(time(NULL));
while (min >= max)
{
printf("Enter min:");
scanf("%d", &min);
printf("Enter max:");
scanf("%d", &max);
}
for (i = 0; i<5; i++)
{
setRandomPoint(&point, min, max);
printf("%d. point.x=%d, point.y=%d\n", i + 1, point.x, point.y);
}
printf("Finish.\n");
return 0;
}
please anyone can help me plz ?
Upvotes: 3
Views: 26949
Reputation: 213711
struct mypoint;
This is a forward declaration. You tell the compiler that there exists a struct called mypoint
but for now the compiler can't know what that struct contains. This is a struct of incomplete type, meaning that somewhere later you have to complete it with a full definition.
typedef struct mypoint {
int x;
int y;
};
This isn't a correct way to define a struct. If you omit the typedef, then you would get a struct that's defined with a "tag" called mypoint
. That would be enough to use said struct, even though you'll have to refer to it as struct mypoint
everywhere.
You should only use typedef
when making a new struct type. Such a type needs a type name, which is typed at the end. The above code snippet is missing the type name, which is why you get the compiler error.
Correct code:
typedef struct mypoint { // struct "tag"
int x;
int y;
} mypoint; // type name
Note that the struct tag and type name exist in different name spaces, so they can actually have the same name. Most often both of these are not used at the same time. Common styles is to either use typedef struct {...} mypoint;
or struct mypoint {...};
.
There's a bit of a style war about what's preferred. The Linux camp prefer the latter style and therefore types out struct
everywhere in the code. While pretty much everyone else prefers to use typedef. The arguments for or against either style are subjective and there is no obvious right or wrong.
Upvotes: 4
Reputation: 5011
Add a name to your typedef
'ed struct :
typedef struct mypoint {
int x;
int y;
} myPoint;
Judging from your statement :
struct myPoint point;
I guess the name you want to give is myPoint
. Right now, you have forgotten to give it a name so myPoint
does not exist.
Also note that the declaration :
struct myPoint point;
which you are now using is wrong, even if you give a name to your typedef
'ed struct. After giving a name, you can either access your struct
by using :
myPoint point;
or :
struct mypoint point;
Upvotes: 5
Reputation: 3226
In typedefed struct
you forgot the name myPoint
typedef struct mypoint {
...
...
} myPoint;
Also, later use myPoint point;
only as you already defined myPoint
as struct. So no need to use struct
again
Also, you can get rid of the statement struct mypoint;
Upvotes: 0