Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a linked list to a function

Status
Not open for further replies.

carlg

Programmer
Jun 23, 2004
88
0
0
US
I am trying to pass a pointer to a linked list to a function. I want the function to modify the linked list.

In the function, If I print out sp->sv_id, I get a good value, but in main if I print out pointer->sv_id, I get junk.

Since pointer is a pointer, shouldn't it get modified by the function?

Thanks for the help
Carl






#include <stdio.h>
#include <string.h>

struct ActiveServices{
int sv_id;
struct ActiveServices *next;
};

int myfunct (struct ActiveServices *sp);
int main ()
{


int index;
struct ActiveServices *pointer;

myfunct(pointer);

printf ("IN MAIN IT IS %d\n", pointer->sv_id);
return (0);
}

int myfunct (struct ActiveServices *sp)
{
int count;
struct ActiveServices *p;

sp = (struct ActiveServices *)malloc(sizeof(struct ActiveServices));

sp->next = 0;

p=sp;

for (count=0;count<=13;count++)
{
p->sv_id=count;
p->next = (struct ActiveServices *)malloc(sizeof(struct ActiveServices));
p = p->next;
}
p=sp;
return (0);
}
 
No, you are passing the pointer by value.
myfunct() just makes a copy of the address stored in pointer (which happens to be junk since you didn't initialize it).

If you want a function to be able to change a pointer, you have to pass it by reference. Ex.
Code:
int main()
{
   ...
   myfunct( &pointer );
   ...
}

int myfunct (struct ActiveServices **sp)
{
   ...
}
Then make all the required changes to myfunct() to de-reference the pointer-to-pointer properly.
 
Thanks!
So how should "pointer" be declared?

struct ActiveServices *pointer;


or

struct ActiveServices pointer;


 
In main(), the pointer should be declared just the way it is now.

The only difference is that you were declaring a pointer to ActiveServices, which creates a 4-byte address buffer on the stack. Normally, you'd malloc() some memory on the heap and store the address of that memory in the pointer.
Since the function creates a new 4-byte address buffer on the stack and copies the pointer value, any change you make to the pointer is not reflected outside of that function (but if you change data on the Heap where the pointer is pointing to, you'll see those changes outside the function).

So now you're taking the address of the 4-byte Stack buffer and passing that to the function. Now that the function has the address of your pointer, it can modify the actual pointer itself and not just what it's pointing to.
 
Still think I'm missing something;
I keep getting memory fault when I run






#include <stdio.h>
#include <string.h>

struct ActiveServices{
int sv_id;
struct ActiveServices *next;
};

int myfunct (struct ActiveServices **sp);
int main ()
{


int index;
struct ActiveServices *pointer;

myfunct(&pointer);


return (0);
}

int myfunct (struct ActiveServices **sp)
{
int count;
struct ActiveServices *p;

sp = (struct ActiveServices **)malloc(sizeof(struct ActiveServices));

/*sp->next = 0;*/

p=*sp;

for (count=0;count<=13;count++)
{
p->sv_id=count;
p->next = (struct ActiveServices *)malloc(sizeof(struct ActiveServices));
p = p->next;
}
/*p=sp;*/

return (0);
}
~
 
Please use the [code][/code] tags when posting code.

> sp = (struct ActiveServices **)malloc(sizeof(struct ActiveServices));
If you include stdlib.h, you don't need the cast.
Casting hides the fact that this wasn't done.

Also, you have a pointer to a pointer, so you should have
Code:
*sp = malloc(sizeof(struct ActiveServices));

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top