3 Replies - 892 Views - Last Post: 03 December 2007 - 03:07 PM Rate Topic: -----

#1 nazzan1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-October 07

Question about pointers

Posted 02 December 2007 - 08:38 PM

This might be totally asinine, but I'm trying to get a hold of pointers in c and was wondering the following:
I found this explanation of declaring and using pointers to structs on some site or another (put it in a code box for readability)

15.4: Pointers to Structures

[This section corresponds to K&R Sec. 6.4]

Pointers in C are general; we can have pointers to any type. It turns out that pointers to structures are particularly useful.

We declare pointers to structures the same way we declare any other pointers: by preceding the variable name with an asterisk in the declaration. We could declare two pointers to struct complex with

	struct complex *p1, *p2;

And, as before, we could set these pointers to point to actual variables of type complex:

	p1 = &c2;
	p2 = &c3;

Then,

	*p1 = *p2

would copy the structure pointed to by p2 to the structure pointed to by p1 (i.e. c3 to c2), and

	p1 = p2

would set p1 to point wherever p2 points. (None of this is new, these are the obvious analogs of how all pointer assignments work.) If we wanted to access the member of a pointed-to structure, it would be a tiny bit messy--first we'd have to use * to get the structure pointed to, then . to access the desired member. Furthermore, since . has higher precedence than *, we'd have to use parentheses:

	(*p1).real

(Without the parentheses, i.e. if we wrote simply *p1.real, we'd be taking the structure p1, selecting its member named real, and accessing the value that p1.real points to, which would be doubly nonfunctional, since the real member is in our ongoing example not a pointer, and p1 is not a structure but rather a pointer to a structure, so the . operator won't work on it.)

Since pointers to structures are common, and the parentheses in the example above are a nuisance, there's another structure selection operator which works on pointers to structures. If p is a pointer to a structure and m is a member of that structure, then

	p->m

selects that member of the pointed-to structure. The expression p->m is therefore exactly equivalent to

	(*p).m



So, suppose I have declared two structs (as I have for an assignment) as follows:


struct point {
double x;
double y;
};

struct line {
struct point a;
struct point b;
};




in the assignment, I need to call a function whose prototype is
distance_between_points(struct point *p1, struct point *p2).
and give it the components of a struct line pointer (don't understand why, but it HAS to be this way for the assignment). I guess my question is whether there is a way to cast the components of a struct line (which are struct points) into POINTERS to struct points while passing them to a function.

right now, I'm passing the components of line *l as follows
distance_between_points(l->a, l->b);


and getting an error because "a" and "b" are struct points and not pointers to struct points (but I'm not allowed to change the definition of "struct line" to make them pointers) so can I somehow cast them as pointers when passing them to "distance_between_points".
Thanks, and I sincerely hope this is a question with an obvious and simple answer.
Maybe I have some fundamental misunderstanding about the nature of pointers vs. variables.

This post has been edited by nazzan1: 02 December 2007 - 08:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Question about pointers

#2 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: Question about pointers

Posted 02 December 2007 - 09:18 PM

A pointer is an address in memory. You declare and dereference a pointer by using '*'. Dereferencing means that you get the value stored at a particular location in memory. The opposite of dereferencing is getting the address which is done by using the '&.'
What I would do is
typedef struct POINT
{
	double x, y;
} point;

typedef struct LINE
{
	point* a, b;
}

distanceBetweenPoints(point* a, point* b)
{
	//code here
}

int main(void)
{
	point a, b;
	a.x = a.y = 0;
	b.x = b.y = 3;
	line l;
	l.a = &a;
	l.b = &b;
	distanceBetweenPoints(l->a, l->b);
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: Question about pointers

Posted 03 December 2007 - 04:03 AM

View Postnazzan1, on 3 Dec, 2007 - 03:38 AM, said:

in the assignment, I need to call a function whose prototype is
distance_between_points(struct point *p1, struct point *p2).
and give it the components of a struct line pointer (don't understand why, but it HAS to be this way for the assignment). I guess my question is whether there is a way to cast the components of a struct line (which are struct points) into POINTERS to struct points while passing them to a function.

No casting needed, just use the address-of operator, &

	struct line my_line;
	/* ...  */
	distance_between_points( &my_line.a, &my_line.b );



View PostGWatt, on 3 Dec, 2007 - 04:18 AM, said:

A pointer is an address in memory. You declare and dereference a pointer by using '*'. Dereferencing means that you get the value stored at a particular location in memory. The opposite of dereferencing is getting the address which is done by using the '&.'

Right, except that the code example you gave doesn't compile. I can see at least 4 mistakes in your code without even trying to put it through my compiler (Which would probably find more), please try actually running and testing your code before pasting it as an example in reply to a help thread.

This post has been edited by Bench: 03 December 2007 - 04:09 AM

Was This Post Helpful? 0
  • +
  • -

#4 nazzan1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-October 07

Re: Question about pointers

Posted 03 December 2007 - 03:07 PM

Thanks for the clarification. Think I'll be able to get this running!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1