Mail Archives: djgpp/1999/10/25/11:55:34
On Mon, 25 Oct 1999 05:31:39 GMT, kezman AT nOsPaMbigfoot DOT com (Kieran
Farrell) wrote:
>Heya peeps,
>
>I need a liitle help, I am doing a complex flat file database using
>linked lists and I can't think for some reason. Please look at the
>following segment.
[snipped]
>OK this is the sort of thing I'm trying to do, however when I allocate
>memory and leave the function it destroys the memory and Start in main
>is still NULL. This suggests to me that I'm passing by value and not
>by reference right?
>
>So how would I pass the pointer to the structure by refference. As a
>quick fix I have done the following.
[snipped again]
To pass a pointer by reference, you pass a pointer to the pointer. For
eg (and this isn't tested):
--- begin
#include <stdio.h>
void AllocInt( int **x )
{
*x = malloc(sizeof(int)); /* allocate space for an int */
**x = 5; /* set the value to 5 */
}
int main()
{
int *a;
AllocInt(&a);
printf( "*a is: %d\n", *a );
return 0;
}
--- end
Hope this helps. Structures are a little more complex as you have to
do stuff like (*myvar)->member = ....
Davin.
__________________________________________________________
*** davmac - sharkin'!! davmac AT iname DOT com ***
my programming page: http://yoyo.cc.monash.edu.au/~davmac/
- Raw text -