Char* Pointer Returns

This is the function to return all the names of my list nodes,
only the first letter is returned to the console.



Fred
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void LeafList::ListAllLeafs()
{
        LeafNode* newLeaf;
 
	if (!isEmpty())
	{
	        cout << "Start of List\n";
		newLeaf = startOfList;
		while (newLeaf != NULL)
		{
			cout << *(newLeaf->getName());
			cout << "Next Leaf is: " << *(newLeaf->getName()) << endl;
			newLeaf = newLeaf->getNext();
		}
		cout << "End Of List\n\n";
		cout << "Reverse List\n";
		newLeaf = endOfList;
		while (newLeaf != NULL)
		{
			cout << "Previous Town is: " << *(newLeaf->getName()) << endl;
			newLeaf = newLeaf->getPrevious();
		}
		cout << "Start Of List" << endl;
	}
	else
	{
		cout << "List is Empty\n\n";
	}
}



Also reversing the list only returns the first node
Kind Regards,
Last edited on
Please edit your post and put it in between tags ["code] code goes here ["/code"] Without the quotations marks.
Done.
cout << *(newLeaf->getName());

Assuming getName() returns a char*, this means that *getName() would be a char. IE: a single character.

If you want to print the whole string, just print the char*. IE, get rid of that *:

cout << newLeaf->getName();

Also... why char* and not string?
Thankyou.

if you remove the *asterisks will that just not output a hexadecimal number to the screen?

and char* is the variable told to use.
if you remove the *asterisks will that just not output a hexadecimal number to the screen?

If getName() returns a char* then no, it won't output a hex number. That's because ostream has specific code for handling a char* - it assumes that it points to a C string and prints it. For any other type of pointer it will print the value of the pointer.
Nah it's different when using char pointers

char pointers are usually strings so std::cout << char*; doesn't print the location but all single characters excluding the terminate character('\0')

the operator overloading function may look like this:
1
2
3
4
5
std::ostream& operator<<(std::ostream& cout, const char* cstring)
{
    for(int i = 0;cstring[i] != 0; ++i)
        cout << cstring[i];
}
Last edited on
1
2
3
4
char* LeafNode::getName()
{
		return leafName;
}


There is the getname() function code, removing the asterisks still only printed the first letter of the leafs name.
removing the asterisks

You mean "asterisks in the ListAllLeafs()"?

What is the type of LeafNode::leafName and how do you set its value?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void LeafList::addLeafAtEnd(char* name)
{
	cout << "adding Leaf " << name << " at end\n";
	LeafNode* newLeaf;

	if (NULL == endOfList) // is the list empty
	{
		newLeaf = new LeafNode(name);
		startOfList = newLeaf;
		endOfList = newLeaf;
	}
	else
	{
		newLeaf = new LeafNode(name);
		endOfList->setNext(newLeaf);
		newLeaf->setPrevious(endOfList);
		endOfList = newLeaf;
		
	}
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstdlib>
#include "LeafNode.h"
#include "LeafList.h"

using namespace std;

int main(void)
{
	LeafList* leafs = new LeafList();
	leafs->addLeafAtEnd("Leaf one");
	leafs->addLeafAtEnd("Leaf Two");
	leafs->addLeafAtEnd("Leaf Three");
	leafs->addLeafAtEnd("Leaf Four");
	leafs->ListAllLeafs();
	system("Pause");
	return 0;
}

This sets the leaf list, but then only returns L and not the full names of the leafs set?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#pragma once
class LeafNode
{
private:
	char* leafName;
	LeafNode* next;
	LeafNode* previous;
public:
	LeafNode();
	LeafNode(char* leafName);
	LeafNode* getNext();
	LeafNode* getPrevious();
	char* getName();
	void setPrevious(LeafNode* newNode);
	void setNext(LeafNode* newNode);
	~LeafNode();
};

#include "LeafNode.h"
#include <iostream>

LeafNode::LeafNode()
{
	leafName = NULL; // sets leafname to default null
	next = NULL; // intialisies next to null
}

LeafNode::LeafNode(char* name)
{
	leafName = new char(*name); //store new leaf name into contents
	next = NULL; // intialise next to null
}

void LeafNode::setNext(LeafNode* nextNode)
{
	next = nextNode;
}

void LeafNode::setPrevious(LeafNode* previousNode)
{
	previous = previous;
}

LeafNode* LeafNode::getPrevious()
{
	return previous;
}

LeafNode* LeafNode::getNext()
{
	return next;
}

char* LeafNode::getName()
{
		return leafName;
}

LeafNode::~LeafNode()
{
}
#pragma once
#include "LeafNode.h"
class LeafList
{
private:
	LeafNode* startOfList;
	LeafNode* endOfList;
public:
	LeafList();
	bool isEmpty();
	void addLeafAtEnd(char* name);
	void deleteLeaf(char* name);
	void ListAllLeafs();
	~LeafList();
};

#include "LeafList.h"
#include "LeafNode.h"
#include <iostream>

using namespace std;


LeafList::LeafList()
{
	startOfList = NULL; // Set Defaults To Null
	endOfList = NULL;
}

bool LeafList::isEmpty()
{
	return startOfList == NULL;
}

void LeafList::addLeafAtEnd(char* name)
{
	cout << "adding Leaf " << name << " at end\n";
	LeafNode* newLeaf;

	if (NULL == endOfList) // is the list empty
	{
		newLeaf = new LeafNode(name);
		startOfList = newLeaf;
		endOfList = newLeaf;
	}
	else
	{
		newLeaf = new LeafNode(name);
		endOfList->setNext(newLeaf);
		newLeaf->setPrevious(endOfList);
		endOfList = newLeaf;
		
	}
}

void LeafList::ListAllLeafs()
{
	LeafNode* newLeaf;

	if (!isEmpty())
	{
		cout << "Start of List\n";
		newLeaf = startOfList;
		while (newLeaf != NULL)
		{			
			cout << "Next Leaf is: " << *newLeaf->getName() << endl;
			newLeaf = newLeaf->getNext();
		}
		cout << "End Of List\n\n";
		cout << "Reverse List\n";
		newLeaf = endOfList;
		while (newLeaf != NULL)
		{
			cout << "Previous Town is: " << *(newLeaf->getName()) << endl;
			newLeaf = newLeaf->getPrevious();
		}
		cout << "Start Of List" << endl;
	}
	else
	{
		cout << "List is Empty\n\n";
	}
}


LeafList::~LeafList()
{
}
Please explain the line 30 and its implications to copying and destroying a LeafNode.
In LeafNode::LeafNode() you need to initialize previous also.

Since you're storing a raw C-string, you need to ask yourself "who owns the memory that it points to?" The answer should probably be "the leaf owns it." If that's true then you need to delete the memory when you destroy a leaf, and copy the memory if you ever make a copy of a leaf. And you should comment getName() to indicate that the pointer returned is valid as long as the Leaf node exists, and that the caller shouldn't change the memory pointed to.

Whew! That's a whole lot of housekeeping! And a lot of opportunity to get it wrong and introduce a bug. This is why we have std::string. If you use it instead then all these problems basically go away.

For example, LeafNode::LeafNode(char *name) is wrong:
leafName = new char(*name);
This creates a single new character, initializes it with the first character of name and points leafName to it. If you use C-strings, this should be:
leafName = strdup(name);
and LeafNode::~LeafNode() should contain free(leafName);
and you should define a copy constructor and assignment operator, or declare them private so they won't be used.

Again, for all these reasons, you're much better off using std::string for the name.
Topic archived. No new replies allowed.