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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

linked list in function 1

Status
Not open for further replies.

batwill

Programmer
Feb 7, 2004
3
US
I am at the beginning of creating a linked list but program is not working properly because address "list" in main is not getting the value I am assigning it inside of function. What am I doing wrong please help.

#include <iostream>
#include <string>


using namespace std;

struct node
{
string ssno;
string lastName;
string firstName;
int age;
float height;
node *next;
};

void main()
{
void createlist(node *);


node *list = NULL;
createlist(list);
cout << list->ssno;

}
void createlist(node *list)
{
node *temp;
temp=new node;
temp->ssno=&quot;123-45-6789&quot;;
temp->next=list;
list = temp;

}
 
you need to pass the pointer by reference. I'll leave it to you to work out why!
 
I still need help. I thought I was passing by reference. Pointers only hold addresses. When inside function assigning address of temp to main. list outside of function not taking address staying null. When inside function assigning address of list to temp->next has right value. I know I am doing something wrong but don't know what.
 
void func(int*);

is the int* passed by value or reference?

void func(int** ptr);

is ptr passed by reference?
is *ptr passed by reference?

void func(int*&);

Whats happening here?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top