Scope problem? With string decleration.

I

israphelr

Hi people,

I am in the process of writing a linked list (for a learning exercise)
and below is what I've written so far.

When I declare a string variable outside of any functions, the program
works as expected, however once I declarea string in the main
function, when I run the program I get a segmentation fault. I needn't
even do anything with the string, all I have to do is declare it, then
run it/


/
*************************************************************************/

#include <iostream>
#include <string>

using namespace std;

struct node
{
string data;
node *next; //pointer to a node.
};

typedef node* PNode;

void initializeList(PNode* a);
void addNode(PNode* a);
void displayList(PNode a);
void deleteNode(PNode *a);
//bool isInList(string b); IMPLEMENT LATER.

string s;//works if declared outside main as a global, but not inside
main.

int main()
{
PNode head;
char choice;

initializeList(&head);
do {
addNode(&head);
cout << "Another? 'Y' for yes, Press 'q' to quit\n";
cin >> choice;
} while (choice != 'q' && choice != 'Q');
displayList(head);
deleteNode(&head);
displayList(head);
cout << "Enter the name you would like to search:\n";
return 0;
}

void initializeList(PNode* a)
{
a = NULL;
}

void addNode(PNode* head)
{
PNode tmp; //new node.

tmp = new node; //create a new node and point to it using tmp.
cout << "Enter your name: \n";
cin >> tmp->data;
if (*head == NULL)
{
tmp->next = NULL;
*head = tmp;
}
else
{
tmp->next = *head; //temp node point to the node pointed to by
head
*head = tmp; //head points to the first node
}

}

void displayList(PNode head)
{
PNode current;

current = head;
do {
cout << current->data << endl;
current = current->next;
} while (current->next != NULL);
}

//list is not empty/has more then 1 node, but will deal with this
later.
void deleteNode(PNode *a)
{
PNode current, previous;

//delete from front.
current = *a; //current now points to what head points to.
*a = current->next; //head points to the node pointed to by current.
delete current;
current = NULL;
}

/
*************************************************************************/

Thanks in advance for your help.
 
I

israphelr

Hi people,

I am in the process of writing a linked list (for a learning exercise)
and below is what I've written so far.

When I declare a string variable outside of any functions, the program
works as expected, however once I declarea string in the main
function, when I run the program I get a segmentation fault. I needn't
even do anything with the string, all I have to do is declare it, then
run it/

/
*************************************************************************/

#include <iostream>
#include <string>

using namespace std;

struct node
{
string data;
node *next; //pointer to a node.

};

typedef node* PNode;

void initializeList(PNode* a);
void addNode(PNode* a);
void displayList(PNode a);
void deleteNode(PNode *a);
//bool isInList(string b); IMPLEMENT LATER.

string s;//works if declared outside main as a global, but not inside
main.

int main()
{
PNode head;
char choice;

initializeList(&head);
do {
addNode(&head);
cout << "Another? 'Y' for yes, Press 'q' to quit\n";
cin >> choice;
} while (choice != 'q' && choice != 'Q');
displayList(head);
deleteNode(&head);
displayList(head);
cout << "Enter the name you would like to search:\n";
return 0;

}

void initializeList(PNode* a)
{
a = NULL;

}

void addNode(PNode* head)
{
PNode tmp; //new node.

tmp = new node; //create a new node and point to it using tmp.
cout << "Enter your name: \n";
cin >> tmp->data;
if (*head == NULL)
{
tmp->next = NULL;
*head = tmp;
}
else
{
tmp->next = *head; //temp node point to the node pointed to by
head
*head = tmp; //head points to the first node
}

}

void displayList(PNode head)
{
PNode current;

current = head;
do {
cout << current->data << endl;
current = current->next;
} while (current->next != NULL);

}

//list is not empty/has more then 1 node, but will deal with this
later.
void deleteNode(PNode *a)
{
PNode current, previous;

//delete from front.
current = *a; //current now points to what head points to.
*a = current->next; //head points to the node pointed to by current.
delete current;
current = NULL;

}

/
*************************************************************************/

Thanks in advance for your help.

Sorry, the problem with this was to do with initializing the head of
the list, Instead of setting head to point to null, I set the pointer
to head to point to null.
 
I

israphelr

Sorry, the problem with this was to do with initializing the head of
the list, Instead of setting head to point to null, I set the pointer
to head to point to null.

And by the way, if anyone is able to explain to me why the positional
decleration of a string variable should have anything to do with,
this, that would be very much appreciated also.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top