Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,131
4,110
5045 feet above sea level
Hi all, I can not seem to get this to compile. Any ideas on what could be tripping it up? This is a .h file that I am trying to create on my own

struct point
{
double x;
double y;
};

struct line
{
struct point p1;
struct point p2;
};

double length(line *1); //function prototype
double distance(point *p1, point *p2); //function prototype


Thanks for any help you can offer

it says there is a syntax error before the a token on my function prototype lines
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
double length(struct line *1);

…and so on. Also the "*1" there looks like a typo.
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,131
4,110
5045 feet above sea level
double length(struct line *1);

…and so on. Also the "*1" there looks like a typo.

Thanks however I was told to use these function prototypes exactly. I had to define 2 structures, one that is point which has x and y and one that is line which contains 2 points.

Thanks for the help nonetheless!
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
Thanks however I was told to use these function prototypes exactly.
Then the instructions have a problem. You won't be able to use those function prototypes without the struct keyword, unless you also add typedefs.

Also, *1 is plain old wrong, a variable name can't be numeric.


Edit: One thing concerns me here, is that the header file you posted at the top of the thread would not run into this issue if it was supposed to be C++ (it does have the problems you reported as C, though). What language are you supposed to be learning here?
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,131
4,110
5045 feet above sea level
Then the instructions have a problem. You won't be able to use those function prototypes without the struct keyword, unless you also add typedefs.

Also, *1 is plain old wrong, a variable name can't be numeric.

cool thanks, typedef worked. Also I tried *l as in "ell" rather than the number and it worked too. They look so similar lol

I have another question if you would be so kind. When I coompile my c file that makes use of the h file, I have a fuction that attempts to calculate the x coordinate difference of two points.

for example

double deltax=*p1.x-*p2.x;

however, the compiler returns the message "request for member a in something not a structure or union"

But isn't it though, since p1 is a point which in turn has x and y components in the point structure?

Once again, you help is grealty appreciated
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
I'm kind of guessing without seeing the rest of the code, but the *p1.x probably wants to be either (*p1).x or the more conventional p1->x

The parens are necessary in the first form of a pointer into a struct, because . has higher precedence than * . (compiler doesn't know what to make of the * until too late).
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,131
4,110
5045 feet above sea level
Thanks that was the issue.

Got another question after playing around with it last night and this morning.

in my function,

double length(line *l)
{
double value;
value=distance((*l).(*p1), (*l).(*p2));

return value;
}

the compiler gets hung up on the line value=distance line

it says there is a syntax error before the a token.

am i correct in thinking (*l).(*p1) will point to point one on the first line entered and (*l).(*p2) will point to the second point of the first line? Then the distance fucntion would manipulate the points?

Thanks for the insight you have provided
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,131
4,110
5045 feet above sea level
This didn't work. Says I have an incompatible type for argument 1 of a and incompatible type for arguement 2 of a.

how would I reference a pointer, line, which is a struct of points which are also a struct consisting of x and y values?

many thanks for the help, especially to iMeowbot and pilotError
 
cool thanks, I'll try that when I get back to my computer

just wondering though, would that be equivalent to

(*l).p1 as in the same as l->p1 ?

Yes, both (*l). and -> dereference the pointer.

Your problem lies within your struct declaration for the line. The two variables within the struct are not pointers. Two ways of solving this. One way is to make the point variables pointers:

typedef struct point {
double x;
double y;
} point;

typedef struct line {
point * p1;
point * p2;
} line;

However, with this solution, when you make a line object you will need to use malloc to allocate memory for the points.

Or you can leave your struct be and change the arguments to be a plain point, instead of point *. But I believe it's more efficient to use pointers, as they would use less memory.

I hope I was clear.
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
Yes, both (*l). and -> dereference the pointer.

Your problem lies within your struct declaration for the line. The two variables within the struct are not pointers. Two ways of solving this. One way is to make the point variables pointers:

Since he's apparently working from a book, I would guess that the structs are a given. Sounds to me like the distance function is expecting pointers to p1 and p2, not actually p1 and p2.

In that case: distance(&(l->p1), &(l->p2))
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Hi all, I can not seem to get this to compile. Any ideas on what could be tripping it up? This is a .h file that I am trying to create on my own

What programming language are you using? Out of C, C++ and Objective C, only one would accept this header file.
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,131
4,110
5045 feet above sea level
thanks MacCoaster and Savar declaring them as pointers in the struct let it compile. I will need to work with malloc to store the pts for each line I assume.

Gnasher729, this is in C by the way

Once again thanks and I may need to ask for your assistance again in the near future!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.