Very confusing about passing values through pointer array among functions?? :-(

This is a discussion on Very confusing about passing values through pointer array among functions?? :-( within the C++ Programming forums, part of the General Programming Boards category; Hi, I think my concept has a bug that I cannot handle the pointer array passing throughout function very well. ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Question Very confusing about passing values through pointer array among functions?? :-(

    Hi,

    I think my concept has a bug that I cannot handle the pointer array passing throughout function very well. Take the following code as an example:

    #include <iostream.h>
    #include <string.h>

    class books
    {
    private:
    char author[30];
    char title[30];

    public:
    books() //constructor1
    {
    strcpy(author, "none");
    strcpy(title, "none");
    };

    books(char auth[30], char tit[30]) //constructor2
    {
    strcpy(author, auth);
    strcpy(title, tit);
    };

    ~books()
    {
    cout << "Destructor: " << author << endl << endl;
    };

    void set_author(char s[30]);
    void set_title(char s[30]);

    void* pointer(const char* a[30]); //Am I right or wrong?
    void* pointer1(const char* t[30]);

    void display();
    };

    void books::set_author(char s[30])
    {
    strcpy(author, s);
    }

    void books::set_title(char s[30])
    {
    strcpy(title, s);
    }

    void* books:ointer(const char* a[30]) // Am I right or wrong?
    {
    strcpy(author, *a);
    return a; //How should I set the return value?
    }

    void* books:ointer1(const char* t[30])
    {
    strcpy(title, *t);
    return t;
    }

    void books::display()
    {
    cout << author << " , " << title << endl << endl;
    }


    int main(int argc, char* argv[])
    {
    books bk1, bk2, bk3("Paul", "Story book");

    bk1.set_author("Raymond"); //through base class public function to access the data members

    bk1.set_title("Eye on HK");

    bk1.display();

    bk2.display(); //constructor 1 (call constructor 1)

    bk3.display(); //constructor 2 (call constructor 2)

    books *bk4 = new books("Peter", "Maths"); //Dynamic pointer and allocate a memory space

    bk4->display();

    delete bk4;

    /*char x[] = "ABC";

    books *bk5;*/ // Don't know how to pass char array "ABC" to the void pointer function above to set the author name???

    return 0;
    }


    Also, is there no such thing: reference array? e.g. &x[10].

    If so, reference will only deal with single int, char or string but not array??

    There is pass by value - the local variables in main will not change.

    pass by reference - the local variables in main will change.

    How about pointer? Is it a kind of pass by reference? I messed up these stuff. Anyone could give me an example like the above how to pass pointer between main and function? and when should I use it?

    Thank you very much.

    gogo




  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    #icnlude <iostream.h>
    #icnlude <string.h>

    void changeName(char*);

    int main()
    {
    char * name = new char[80];
    strcpy(name, "Howdy Doody");
    changeName(name);
    cout << name;
    delete [] name;
    name = NULL;
    return 0;
    }

    void changeName(char * Name)
    {
    strcpy(Name, "Walker, Texas Ranger");
    }

    should put Walker, Texas Ranger to the screen.

    Use pass by value if you don't want function to change value of passed variable and pass by reference using a reference or a pointer if you do want to change the value of the passed variable or if the variable is large and therefore you save time (not much, but a little) by not having to create a copy of the passed variable to use in the function.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    char * name = new char[80];
    strcpy(name, "Howdy Doody");
    changeName(name);

    I found that the above code cannot change the name of the function and name cannot be null as it is not int.

    I want to know that:

    When I created a private data member which is char array type like my example: char author[30], char title[30], and I have a function which accepts pointer notation when passing arrays like

    void point(char* x, char* y)
    {
    author = x;
    title = y; // I knew they are wrong and how can I set them?
    }

    and in the

    int main()
    {
    //How can I pass the char pointer array to the above function which in turn change the values of private data members: author[30] and title[30]?

    books object, *ptr;
    ptr = &object;

    ptr->point("Ivor", "Java 2");

    //I tried this but failed. How should I pass them? I know how to use the dynamic pointer char * name = new char[80] with there are no pointer in the void function. However, how should I set them if there are pointer in the void function? void point(char* x, char* y).

    return 0;

    }

    Pointer + array + passing function + access private data members made me crazy.

    Any help ?????

    Thanks a lot.

    gogo





Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Passing pointer to array to a function
    By Tojam in forum C Programming
    Replies: 1
    Last Post: 10-09-2002, 09:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21