call a function to return a pointer to a pointer to a two dimensional array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: call a function to return a pointer to a pointer to a two dimensional array

  1. #1
    sbear Guest

    call a function to return a pointer to a pointer to a two dimensional array


    I am still having a problem calling this function. I don't know
    what I am doing wrong.
    Here is the code I am working with.
    char *AddName(void);
    void main(void) {
    char Names[4][20];
    char (*Ptr)[20] = Names;
    Ptr = AddName();}
    char*AddName(void)
    {
    char *Nme[4][20];

    cout << "Enter a first and last name: ";
    cin.getline(Nme, 20);

    return Nme;
    }
    I know this is probably simple, but I am just not understanding it.
    I am able to do a one dim array
    easily but when it comes to a two dimensional, I am lost. My reference
    books are wonderful books
    for one dim arrays and pointer, but doesn't expand much on the latter.
    Can someone help?
    Thanks, sbear


  2. #2
    marc Guest

    Re: call a function to return a pointer to a pointer to a two dimensional array


    "sbear" <bjbear1@spacestar.net> wrote:
    >
    > I am still having a problem calling this function. I don't know
    > what I am doing wrong.
    > Here is the code I am working with.
    > char *AddName(void);
    > void main(void) {
    > char Names[4][20];
    > char (*Ptr)[20] = Names;
    > Ptr = AddName();}
    > char*AddName(void)
    > {
    > char *Nme[4][20];
    >
    > cout << "Enter a first and last name: ";
    > cin.getline(Nme, 20);
    >
    > return Nme;
    > }
    > I know this is probably simple, but I am just not understanding it.
    > I am able to do a one dim array
    > easily but when it comes to a two dimensional, I am lost. My reference
    >books are wonderful books
    > for one dim arrays and pointer, but doesn't expand much on the latter.
    >Can someone help?
    >Thanks, sbear
    >


    The biggest issue here is that you are returning a 'stack' variable (goes
    out of scope outside of the function that instatiated it). Try using the
    'static' qualifier in front of Nme. That being said, you still have a few
    other issues to take into consideration. The first is that your getline(
    ) function is always filling in the same memory location, which means that
    using 'static' will have subsequent calls to AddName( ) overwriting earlier
    results (I doubt that's what you are looking for). There are two possible
    solutions: 1) allocate a new memory buffer and return that or 2) strcpy(
    ) the result of AddName( ) into a staticly (or dynamically) defined variable
    in main( ). Just don't forget to free/delete the memory...;)

    Hope that helps...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center