|
-
May 6th, 2000, 04:04 AM
#1
Thread Starter
Dazed Member
Does anyone know a way how i could add another string
to a pointer array???
char* pfirstname[]={
"Chris",
"Mike",
};
I like the idea of using an array of pointers rather than an array, but i have no clue how to do this.
cout << "To enter a new entry press 1"<< endl;
if (selection == 1)
{
cout << endl << "Enter you new name here: ";
char* pfirstname[3];
cout << endl;
}
Am i on the right track???
Thanxxxx.
-
May 7th, 2000, 05:30 AM
#2
Frenzied Member
no is the short answer, I'm not too hot at C++ but look into the new keyword, see MSDN , I havn't got anything working but you need to create a new array with 3 members, copy the old array into it, delete the old array and set the pointer to pfirstname[] to the new array. I'd provide code if I could but I leave my strings in VB where it's all done for me.
-
May 8th, 2000, 03:47 AM
#3
if you want to works with strings why don't you use the string object and then you can dynamically assign array size at runtime using a pointer
string *strptr;
strptr=new string[size];
you can even do what Redim Preserve does by first allocating a temp array copying the contents then resizing the original array and copying the stuff back.
-
May 9th, 2000, 03:39 AM
#4
Lively Member
I'll give you an example of how to do it in ANSI C but I can't right at the moment.. I've been trying to all day but work keeps getting in the way :-).
-
May 10th, 2000, 11:11 PM
#5
Lively Member
ok.. I finally got a few minutes to respond to this. What you're probably going to want to do is create a linked list. The only advantage to an array is that it's contigous memory.. which means faster access. If you're not working with thousands of elements and you're not accessing them thousands of times, a linked list is much easier to manage.. Here's how you'd do it.
#define MAX_NAME_LEN 25
struct namelist {
char firstname[MAX_NAME_LEN + 1];
char lastname[MAX_NAME_LEN + 1]; //you can add more like this
struct namelist *next;
};
struct namelist *mylist, *tmplist;
//Fill the first element of the list
strncpy(mylist->firstname, "Bob", MAX_NAME_LEN);
strncpy(mylist->lastname, "Smith", MAX_NAME_LEN);
//terminate the last element of the chain
mylist->next = NULL;
//make a new element of the list
tmplist = malloc(sizeof(struct namelist));
//could not allocate mem
if (tmplist == 0) {
//do some err logging
exit(1)
}
strncpy(tmplist->firstname, "Jonh", MAX_NAME_LEN);
strncpy(tmplist->lastname, "Doe", MAX_NAME_LEN);
//make the next element in the list the first one we created
tmplist->next = mylist;
//make the new element we created the first in the list
mylist = tmplist;
********************************************
now mylist is pointing to the element that has "John Doe".
This is how you'd go through the list...
********************************************
//Prints out all the names in the list
void Print_Names(struct namelist *thelist) {
struct namelist *listitem = thelist;
while (listitem != NULL) {
printf("%s %s", listitem->firstname, listitem->lastname);
listitem = listitem->next;
}
***********************************************
So you could easily add a function to add the names
***********************************************
int Add_Name(struct namelist *mylist, char *first, char *last) {
struct namelist *tmplist;
tmplist = malloc(sizeof(tmplist));
if (tmplist == 0) {
printf ("Could not allocate memory - trying to add a name");
exit(1);
}
strncpy(tmplist->firstname, first, MAX_NAME_LEN);
strncpy(tmplist->lastname, last, MAX_NAMELEN);
tmplist->next = mylist;
mylist = tmplist;
return 1;
}
-
May 22nd, 2000, 01:16 AM
#6
Monday Morning Lunatic
I hate to say this, but there is a considerably easier
way to do this in C++, with the Standard Template Library.
It comes with a template called vector, which allows lists
of anything, and it handles all the allocation itself.
If you use the STL string class, then that does the same
for text:
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<string> vsNames;
void main(int argc, char** argv) {
vsNames.push_back(string("Chris"));
vsNames.push_back(string("Mike"));
cout << "Size: " << vsNames.size() << endl;
cout << "Item 2: " << vsNames[1] << endl;
}
There - simple! Don't even need to allocate/deallocate
memory! Yeah, it's a little slower than linked lists, but
it's a hell of a lot easier, plus you get all the type
checking done at compile-time, when the compiler expands
the templates.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|