Thread: Struggling on functions and pointers with structures

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    14

    Struggling on functions and pointers with structures

    This week in class we went over structures in class. Boy am I up creek without a paddle... I understand structures and the idea and initializing alright, but I'm completely lost when it comes to the notation of using the structures. Hoping someone can give me insight on what I'm not catching onto here.

    My teacher gave us this sheet of paper that will supposedly help us understand it better. I'm staring at it and I'm lost.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    struct point
    {
      float x;
      float y;
    };
    
    float distance (struct point, struct point);
    void enter_a_point (struct point * );
    
    int main (Void)
    {
      struct point pt1, pt2;
    
      enter_a_point(?);
      enter_a_point(?);
      printf("Distance between the point = %.2f\n", distance(?,?));
    
      return 0;
    }
    
    float distance (struct point p1, struct point p2)
    {
      return sqrt(pow(?,2) + pow(?,2));
    }
    
    void enter_a_point (struct point *p)
    {
      printf("\nEnter an X coordinate: ");
      scanf("%f",?);
    
      printf("\nEnter a Y coordinate: ");
      scanf("%f",?);
    }
    the spots that have to do with the notation have been replaced with "?". Looking at this, this is what I've come up with (which is probably wrong).

    so there's a structure called point that has 2 elements x and y, also we also have 2 variables pt1 and pt2. I can call on these structures with pt1.x, pt1.y, pt2.x, pt2.y.

    I hit the enter_a_point function, which has to be a pointer, to make it easy I call it *pt1, and then another enter_a_pointer function is called I call it *pt2. To point at something the user is asked for a x and y value to be stored with scanf, which I'm kind of stuck at understanding. I can't call it &pt1.x or &pt1.y, because when the function is ran again it will store pt2's data into pt1.

    next it uses the distance function, which is just the distance formula between 2 points, so I have to give it the data stored with the pt1 and pt2, x and y for both cases. I'm so far lost I can't picture or remember what goes where... Also can't find anything in my text or online that seems to help better understand this.

  2. #2
    Registered User
    Join Date
    May 2013
    Posts
    14
    I think I figured some of it out...

    Code:
    void enter_a_point (struct point * );
    is a pointer so I want to give it an address to point to? If that's correct then I give it &pt1 and &pt2

    Code:
     enter_a_point(&pt1);
      enter_a_point(&pt2);
    For this distance formula i need a value for pt1 and pt2, so I just send in what I have stored at those variables...

    Code:
    printf("Distance between the point = %.2f\n", distance(pt1,pt2));
    distance formula is easy I want it to return the answer of my structure values running through it.

    Code:
     return sqrt(pow(pt2.x - pt1.x,2) + pow(pt2.y - pt1.y,2));
    The enter_a_point function has me stumped still. I'm lost at what I put in there... it's looking for a point "p", and it's scanf so it should require &p and then .x and .y with respect to the question.

    Code:
     printf("\nEnter an X coordinate: ");
      scanf("%f",&p.x);
    
      printf("\nEnter a Y coordinate: ");
      scanf("%f",&p.y);
    which is giving me the error...

    Code:
    extra4.c: In function 'enter_a_point':
    extra4.c:32: error: request for member 'x' in something not a structure or union
    extra4.c:35: error: request for member 'y' in something not a structure or union

  3. #3
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by Majorpain2587 View Post
    ...
    so there's a structure called point that has 2 elements x and y, also we also have 2 variables pt1 and pt2. I can call on these structures with pt1.x, pt1.y, pt2.x, pt2.y.
    Perhaps it'll help you understand them better if you think of pt1 and pt2 as variables of type struct point.

    Put in other words, you may think of structures (struct whatever) as custom types. That is, besides the primary types of the language: char, int, float, double.

    The x and y floats defined inside struct point are member-variables of that struct.

    So, once you define some variables of type struct point, like pt1 and pt2 in your case, each one of them will have its own x and y members, accessed with a dot, like this...

    Code:
    ...
    struct point pt1, pt2;
    ...
    pt1.x = 2.00;
    pt1.y = 4.00;
    
    pt2.x = 10.00;
    pt2.y = 12.00;
    ...
    If instead of simple variables you define pt1 and pt2 to be pointers to struct point, then once you have allocated memory for them, you can access their members using the -> notations instead of the dot.

    For example...

    Code:
    struct point *pt1;
    
    pt1 = malloc( sizeof(struct point) );
    /* here you should check if memory allocation has failed */
    
    pt1->x = 2.00;
    pt1->y = 4.00;
    
    /* do anything else you need to do with pt1 here */
    
    free( pt1 );
    ...
    I hit the enter_a_point function, which has to be a pointer, to make it easy I call it *pt1, and then another enter_a_pointer function is called I call it *pt2. To point at something the user is asked for a x and y value to be stored with scanf, which I'm kind of stuck at understanding. I can't call it &pt1.x or &pt1.y, because when the function is ran again it will store pt2's data into pt1.
    The function enter_a_point(struct point *p) is supposed to accept an argument p of type struct point, in order to read values in the x and y members of the argument.

    Since you want those new values of the members to be retained inside the function argument after the function ends, you should pass the address of a struct point variable into the function (hence the pointer declaration for the argument in the function's signature).

    So...
    Code:
    void enter_a_point( struct point *p )
    {
        printf("\nEnter an X coordinate: ");
        scanf("%f", &pt->x);
        printf("\nEnter a Y coordinate: ");
        scanf("%f", &pt->y);
    }
    next it uses the distance function, which is just the distance formula between 2 points, so I have to give it the data stored with the pt1 and pt2, x and y for both cases. I'm so far lost I can't picture or remember what goes where... Also can't find anything in my text or online that seems to help better understand this.
    Since distance() is not supposed to alter the values of its arguments (or it is not supposed to return them back even it was changing them) you should pass it 2 simple struct point variables (that is, not their address), and use the dot notation to access their members inside the function.

    EDIT: We were cross-posting.
    Last edited by migf1; 05-31-2013 at 06:02 PM.

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    14
    Quote Originally Posted by migf1 View Post
    The function enter_a_point(struct point *p) is supposed to accept an argument p of type struct point, in order to read values in the x and y members of the argument.

    Since you want those new values of the members to be retained inside the function argument after the function ends, you should pass the address of a struct point variable into the function (hence the pointer declaration for the argument in the function's signature).

    So...
    Code:
    void enter_a_point( struct point *p )
    {
        printf("\nEnter an X coordinate: ");
        scanf("%f", &pt->x);
        printf("\nEnter a Y coordinate: ");
        scanf("%f", &pt->y);
    }
    sorry it took so long for me to get back, been in the process of moving. Thank you for breaking it down for me. The step by step really helped. To get a better understanding on the -> notation is that just saying it's a pointer to the struct variable pt.x and pt.y? so in otherwords &pt->x is the same as &(*pt.x)?

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Majorpain2587 View Post
    so in otherwords &pt->x is the same as &(*pt.x)?
    No pt->x is equivalent to (*pt).x (the . operator has higher precedence than the * operator). The -> operator is just syntactic sugar.

    Bye, Andreas

  6. #6
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by Majorpain2587 View Post
    ...
    To get a better understanding on the -> notation is that just saying it's a pointer to the struct variable pt.x and pt.y?
    Actually it means that whatever comes before the -> is a pointer to an already allocated struct variable (in this example, the struct variable is the pt variable). Furthermore, whatever comes after the -> is not the struct variable pointed to, but rather a member of that struct variable.

    In other words, when we see something like the following...

    Code:
    p->x  /* or equivalently: (*p).x */
    we understand (expect) both that:

    1. p points to an already allocated struct variable (or more precisely, p points to some memory space that has been already allocated to hold properly laid out data of some struct type)

    2. the already allocated struct variable has a member called x

    I'm not sure if the following code snippet will make it easier to understand what is going on, but here it is anyway...

    Code:
    int main( void )
    {
    	struct Point {
    		float x;
    		float y;
    	};
    
    	struct Point pt = {1.0, 2.0};	/* simple variable of type struct Point */
    	struct Point *p;		/* uninitialized pointer to type struct Point */
    
    	printf( "pt.x = %g\n", pt.x );
    
    #if disabled
    	printf( "p->x = %g\n", p->x );	/* this crashes, because p does not
    					 * point to a valid struct Point variable
    					 * (so, it tries to access the x-member
    					 *  of a non-existant/invalid struct) */
    #endif
    
    	p = &pt;			/* p now points to an already allocated,
    					 * valid struct Point variable, called pt */
    
    	printf("(*p).x = %g\n", (*p).x);/* since p now points to pt, (*p) equals
    					 * to pt and thus (*p).x equals to pt.x
    					 * (it makes perfect sense, but it looks
    					 * so darn ugly) */
    
    	printf( "p->x = %g\n", p->x );	/* since p already points to a valid
    					 * struct variable, we can access the
    					 * x-member of that variable with the
    					 * much more elegant -> notation */
    
    	return 0;  
    }
    In simple cases, like this one, I guess it may be safe to assume that p is itself the struct variable being pointed to. But in reality it is not (it is actually a redirection, that is: p points to a struct variable, and that struct variable has a member called x).

    This does make a difference when linked-lists and similar data structures utilizing nodes come into play. There, it is not safe to assume that p is itself a node of the list, because for example you could use a pointer p for traversing every node in the list.

    so in otherwords &pt->x is the same as &(*pt.x)?
    Actually, &p->x is the same as &((*p).x)

  7. #7
    Registered User
    Join Date
    May 2013
    Posts
    14
    Thank You again Migf1. You've been a huge help, and I'm extremely grateful. The beginning of this course was simple, and now it's getting a lot more in-depth. doing my best just to keep up.

  8. #8
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Glad I helped

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-28-2012, 10:37 PM
  2. Pointers to structures & functions arguments
    By lautarox in forum C Programming
    Replies: 4
    Last Post: 11-17-2008, 12:27 PM
  3. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  4. Replies: 5
    Last Post: 04-11-2002, 11:29 AM
  5. Newbie Help (Arrays, Structures, Functions,Pointers)
    By tegwin in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 06:29 PM