Question 4.8

I have a function which accepts, and is supposed to initialize, a pointer:

	void f(ip)
	int *ip;
	{
		static int dummy = 5;
		ip = &dummy;
	}
But when I call it like this:
	int *ip;
	f(ip);
the pointer in the caller remains unchanged.


Are you sure the function initialized what you thought it did? Remember that arguments in C are passed by value. The called function altered only the passed copy of the pointer. You'll either want to pass the address of the pointer (the function will end up accepting a pointer-to-a-pointer), or have the function return the pointer.

See also questions 4.9 and 4.11.


Read sequentially: prev next up top


This page by Steve Summit // Copyright 1995 // mail feedback