[comp.lang.c] Passing pointers to structures

gwyn@smoke.brl.mil (Doug Gwyn) (01/20/91)

In article <1991Jan19.055923.19423@unicorn.cc.wwu.edu> n8743196@unicorn.cc.wwu.edu (Jeff Wandling) writes:
-	struct node {
-		int key;
-		};
-	foo(h_ptr)
-	struct node *h_ptr;              /*   ?? */
-	{
-		h_ptr->key=10;		/* anything */
-        }	
-	main() 
-	{ 
-	      struct node *head;
-	      foo(head);  OR  foo(&head);   /* ?? K&R 1, p91 says use '&head'
-					       but this isn't the same. Or is
-			                       it?  */
-	}

What you probably wanted to do was:
	main()
	{
		struct node head;	/* NOTE: not just a pointer */
		foo(&head);		/* definitely not the same as
					   foo(head); which would pass
					   the entire structure rather
					   than just a pointer to it */
		return 0;		/* please return exit status */
	}

ttobler@unislc.uucp (Trent Tobler) (01/23/91)

From article <1991Jan19.055923.19423@unicorn.cc.wwu.edu>, by n8743196@unicorn.cc.wwu.edu (Jeff Wandling):
> 
> Example:
> 
> 	struct node {
> 		int key;
> 		};
> 
> 
> 	foo(h_ptr)
> 	struct node *h_ptr;              /*   ?? */
> 	{
> 		h_ptr->key=10;		/* anything */
>         }	
> 
> 
> 	main() 
> 	{ 
> 	      struct node *head;
> 
> 	      foo(head);  OR  foo(&head);   /* ?? K&R 1, p91 says use '&head'
> 					       but this isn't the same. Or is
> 			                       it?  */
> 	}
> 
> 
>   How do I pass the pointer? And in the function "foo", how do I
> access it? I want "head" to be passed by 'reference', but I don't know
>   I have K&R 1 in front of me and I checked the FAQ. If you can cite a 
> 

:::::::::::::::::::::::

If your purpose was to change the variable head (defined in main) to
point to some structure that is created/looked-up in foo:

You must declare foo() with ...

  foo(h_ptr)
	struct node **h_ptr;	/* h_ptr is the address of a pointer to node */

and access h_ptr with ...

	(*h_ptr) = get_head();		/* assign the pointer to some node */
	(*h_ptr)->key = key_value;	/* assign a key to the node */

and call foo with ...

  struct node *head;	/* this will contain the node pointer */
  foo( &head);		/* assign head to the node */

:::::::::::::::::::::::

If your purpose was different, that of simply affecting the elements
**IN** the struct node variable, then this is the format:

You must declare foo() with ...

  foo(h_ptr)
	struct node *h_ptr;	/* h_ptr is a pointer to a node */

and access h_ptr with ...

	h_ptr->key = key_value;	/* assign a value to the key element */

and call foo with ...

  struct node head;	/* declare some space to store key */
  foo( &head);		/* change a field in head */

> -- 
> jeff wandling | western washington university | inet: jeff@arthur.cs.wwu.edu
> cs ugrad      | bellingham, wa 98225 USA      |  n8743196@unicorn.cc.wwu.edu

--
Trent Tobler    - ttobler@csulx.weber.edu

n8743196@unicorn.cc.wwu.edu (Jeff Wandling) (01/24/91)

I have recieved many replies over the past week or so. You have been
very helpful. You need not send any more. Your letters have cleared
up my confusion.

cheers,

-- 
jeff wandling | western washington university | inet: jeff@arthur.cs.wwu.edu
cs ugrad      | bellingham, wa 98225 USA      |  n8743196@unicorn.cc.wwu.edu