DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  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...

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links