Passing pointers to structures

Doug Gwyn gwyn at smoke.brl.mil
Sun Jan 20 19:25:15 AEST 1991


In article <1991Jan19.055923.19423 at unicorn.cc.wwu.edu> n8743196 at 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 */
	}



More information about the Comp.lang.c mailing list