1

Im kinda new to programming, so if someone could help me here id appreciate it!


typedef struct /*struct for circle creation*/
{
  int x_coord;
  int y_coord;
  int radius;
} circle, *circlepointer;

I need a pointer to a circle, and pass that pointer to a function that returns true if the circle radius is >0, and false otherwise. How do i pass my *circlepointer to a function and check this? I should be able to do something like circle.radius > 0, in the function

Hope you can help

2
  • 1
    Why bother with the *circlepointer typedef? All it does it add another type - and that type obfuscates the fact it's a pointer badly enough you have to put pointer in the type name. Commented Sep 30, 2019 at 13:17
  • In what way circlepointer is better than circle*? Commented Sep 30, 2019 at 14:29

2 Answers 2

3

The function can be declared like

int f( circlepointer c );

or

int f( const circle *c );

and called like

circle c = { /*...*/ };

f( &c );

Within the function you can access data members of the object like c->radius or ( *c ).radius.

Instead of the return type int you can also use the C type _Bool. Or you can include the header <stdbool.h> and write the return type as bool.

Pay attention to that these two function declarations

int f( const circlepointer c );

and

int f( const circle *c );

are not equivalent. In the first declaration the pointer itself is constant. In the second declaration the object pointed to by the pointer is constant.

Sign up to request clarification or add additional context in comments.

1 Comment

Just to add to this answer, the radius member may be accessed in function f above through (*c).radius or c->radius.
1

You can declare and define your function in this way

int func(circle * c) {
    return c->radius > 0;
}

Access a member via pointer can be done with -> operator or by dereferencing the pointer and then just use . operator to access its members

(*c).radius 
c->radius

then just call it in main

circle c = {your_x, your_y, your_radius};
func(&c);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.