Thread: A pointer to a member of an array of structures

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    6

    A pointer to a member of an array of structures

    Code:
    int main(void)
    {
       struct circle circle[MAXCIRCLES]
       getCircle(&circle[1]);
    }
    
    void getCircle(struct circle *circ)
    {
       printf("Centre: ");
       scanf("%f, %f", &(*circ).centre.x, &(*circ).centre.y);
       printf("Radius: ");
       scanf("%f", &(*circ).radius);
    }
    I want to pass the circle[1] by reference to the getCircle function. My code works, but the part where i put &(*circ).centre.x etc seems a little weird. Is there a better way of doing it?

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    you can use the '->' operator if you think it's "better"

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    6
    I find this weird: &(*circ).radius

    Because isn't it saying the address of the value pointed to by the circ pointer? Shouldn't circ return the same thing?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by ChwanRen
    I find this weird: &(*circ).radius

    Because isn't it saying the address of the value pointed to by the circ pointer? Shouldn't circ return the same thing?
    It says:

    1) Dereference 'circ', and give me that object.
    2) Now that I have that object, find the radius member of it.
    3) Now give me the address of that member.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Dynamic pointer array in C
    By MacFromOK in forum Windows Programming
    Replies: 14
    Last Post: 04-09-2005, 06:14 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM