Results 1 to 6 of 6

Thread: Arrays of pointers

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Unhappy

    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.

  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    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.

  3. #3
    Guest
    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.

  4. #4
    Lively Member
    Join Date
    Jan 1999
    Location
    Rochester NY, USA
    Posts
    93
    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 :-).

  5. #5
    Lively Member
    Join Date
    Jan 1999
    Location
    Rochester NY, USA
    Posts
    93
    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;
    }




  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width