none
Best Way To Declare char* RRS feed

  • Question

  • What is the best way to declare the following as a pointer, at least with regard to the first array. Unfortunately the two books I have only give examples on an int. So what's best for a char, as in the following...

    char chStr[MAX_PATH][MAX_PATH];

    IOW, I don't care if the second array reamains MAX_PATH, but what if I want the first arry to be dynamic?

    Perhaps if you know several combinations of declaring char pointers you could show them, i.e., single array and double array.

    I'm thinking along the lines of an int *. For example, I get the count of a listbox, then, as I was shown here the other day, I do the following with an int...

    int *iBuff = new int[iCount];
    ...do stuff
    delete[] iBuff;

    So what is the best way to do something similar with type char?
    Lamblion
    Sunday, June 28, 2009 11:39 PM

Answers

  • As I have abover, char chStr[MAX_PATH][MAX_PATH], I want to be able to dynamically define how elelments for the first array, i.e., sometimes it might be char chStr[4][MAX_PATH] or sometimes char chStr[73][MAX_PATH] or sometimes char chStr[1][MAX_PATH].

    Right now I'm just doing char chStr[MAX_PATH][MAX_PATH], but there's no need to allocate the first array to MAX_PATH if I can dynamically cast it to however many items I get from a listbox count.


    Lamblion

    My preference would be to use a vector of strings, but another option is to allocate an array of string pointers, as shown below:
    char **chrStr = new char* [73];
    for (int i = 0; i < 73; i++)
    {
    	chrStr[i] = new char[MAX_PATH];
    }
    strcpy (chrStr[0], "Hello");




    • Marked as answer by Lamblion Monday, June 29, 2009 4:27 PM
    Monday, June 29, 2009 5:39 AM
  • I think you should read some good book on C++ . First thing use pointer if and only if required otherwise use STL classes inside your code.

    int iVar /* declares an int variable */
    int * iVar /* declares a pointer to an int variable */
    int * iVar [5] /* declares an array of 5 pointers, each to an int variable */

    have a look on the sample example


    /**
    Creating a 2D array
    */


    #include <cstdio>


    // Returns a pointer to a newly created 2d array
    // the array has size [height x width]

    int ** createArray ( unsigned height , unsigned width )
    {
     
    int ** array = 0 ;
      array
    = new int *[ height ];

     
    for ( int h = 0 ; h < height ; h ++)
     
    {
        array
    [ h ] = new int [ width ];

       
    for ( int w = 0 ; w < width ; w ++)
       
    {
         
    // fill in some initial values
         
    // (filling in zeros would be more logic, but this is just for the example)
          array
    [ h ][ w ] = w + width * h ;
       
    }
     
    }

     
    return array ;
    }

    int main ()
    {
      cout<<
    "Creating a 2D array\n" ;
      int height = 15 ;
     
    int width = 10 ;
     
    int ** myArray = createArray ( height , width );
     

     
    // important: clean up memory  
     
    for ( int h = 0 ; h < height ; h ++)
     
    {
       
    delete [] myArray [ h ];
     
    }
     
    delete [] myArray ;
      myArray
    = 0 ;
     

     
    return 0 ;
    }

    Thanks
    Rupesh Shukla
    • Marked as answer by Lamblion Monday, June 29, 2009 5:49 PM
    Monday, June 29, 2009 3:34 PM
  • confirmed.
    • Marked as answer by Lamblion Monday, June 29, 2009 4:27 PM
    Monday, June 29, 2009 4:25 PM
  • Your original requirement can be met using a pointer to a fixed size array.

    char (*p)[MAX_PATH];    // Declare a pointer to a fixed size array
    p = new char[73][MAX_PATH];

    To delete you simply do this once
    delete [] p;


    «_Superman_»
    • Marked as answer by Lamblion Tuesday, June 30, 2009 1:37 PM
    Tuesday, June 30, 2009 7:00 AM

All replies

  • Define what you mean by "double array". Do you want a two-dimensional array, or an array of string pointers?
    Monday, June 29, 2009 12:54 AM
  • As I have abover, char chStr[MAX_PATH][MAX_PATH], I want to be able to dynamically define how elelments for the first array, i.e., sometimes it might be char chStr[4][MAX_PATH] or sometimes char chStr[73][MAX_PATH] or sometimes char chStr[1][MAX_PATH].

    Right now I'm just doing char chStr[MAX_PATH][MAX_PATH], but there's no need to allocate the first array to MAX_PATH if I can dynamically cast it to however many items I get from a listbox count.


    Lamblion
    Monday, June 29, 2009 1:06 AM
  • If using C++ use vector<string>.
    Microsoft MVP - Visual C++
    Blog: http://nibuthomas.com
    Posts are provided as is without warranties or guaranties.
    • Proposed as answer by jinzai Monday, June 29, 2009 3:07 PM
    Monday, June 29, 2009 5:08 AM
    Moderator
  • As I have abover, char chStr[MAX_PATH][MAX_PATH], I want to be able to dynamically define how elelments for the first array, i.e., sometimes it might be char chStr[4][MAX_PATH] or sometimes char chStr[73][MAX_PATH] or sometimes char chStr[1][MAX_PATH].

    Right now I'm just doing char chStr[MAX_PATH][MAX_PATH], but there's no need to allocate the first array to MAX_PATH if I can dynamically cast it to however many items I get from a listbox count.


    Lamblion

    My preference would be to use a vector of strings, but another option is to allocate an array of string pointers, as shown below:
    char **chrStr = new char* [73];
    for (int i = 0; i < 73; i++)
    {
    	chrStr[i] = new char[MAX_PATH];
    }
    strcpy (chrStr[0], "Hello");




    • Marked as answer by Lamblion Monday, June 29, 2009 4:27 PM
    Monday, June 29, 2009 5:39 AM
  • Thanks, but then how do you delete the new char afterwords? by iterating throught it the same way, or no? I tried your example in my code and it works, but I'm not sure how to delete[]. Here's what I've done on that...

    for(int i = 0; i < lrSelNum; i++)
         delete[] chStr[i];

    delete[] chStr; // for the initial declaration

    Is this correct?

    Lamblion
    Monday, June 29, 2009 1:48 PM
  • I think you should read some good book on C++ . First thing use pointer if and only if required otherwise use STL classes inside your code.

    int iVar /* declares an int variable */
    int * iVar /* declares a pointer to an int variable */
    int * iVar [5] /* declares an array of 5 pointers, each to an int variable */

    have a look on the sample example


    /**
    Creating a 2D array
    */


    #include <cstdio>


    // Returns a pointer to a newly created 2d array
    // the array has size [height x width]

    int ** createArray ( unsigned height , unsigned width )
    {
     
    int ** array = 0 ;
      array
    = new int *[ height ];

     
    for ( int h = 0 ; h < height ; h ++)
     
    {
        array
    [ h ] = new int [ width ];

       
    for ( int w = 0 ; w < width ; w ++)
       
    {
         
    // fill in some initial values
         
    // (filling in zeros would be more logic, but this is just for the example)
          array
    [ h ][ w ] = w + width * h ;
       
    }
     
    }

     
    return array ;
    }

    int main ()
    {
      cout<<
    "Creating a 2D array\n" ;
      int height = 15 ;
     
    int width = 10 ;
     
    int ** myArray = createArray ( height , width );
     

     
    // important: clean up memory  
     
    for ( int h = 0 ; h < height ; h ++)
     
    {
       
    delete [] myArray [ h ];
     
    }
     
    delete [] myArray ;
      myArray
    = 0 ;
     

     
    return 0 ;
    }

    Thanks
    Rupesh Shukla
    • Marked as answer by Lamblion Monday, June 29, 2009 5:49 PM
    Monday, June 29, 2009 3:34 PM
  • Thanks, but as I mentioned, the books I've read, and I've read several, don't give examples of type char, only of type int when discussing pointers. Secondly, I don't want to use STL. But it looks like from your example that my delete[] syntax was correct. I would like someone to confirm that, though.
    Lamblion
    Monday, June 29, 2009 4:10 PM
  • confirmed.
    • Marked as answer by Lamblion Monday, June 29, 2009 4:27 PM
    Monday, June 29, 2009 4:25 PM
  • Very much obliged... again.
    Lamblion
    Monday, June 29, 2009 4:27 PM
  • There is no difference if you use char or int pointer concept will be same in both the cases .you just have to go with your concept without worrying that what is the type of pointer . and when you say that anyone confirm your answer i  think you are the one who can confirm it easily use watch window and debug your code and check the value of your pointer nothing else you have to do.Forget about my post whether we both have same syntax for delete or not whether i am using chat or int .Simply debug your code and you will get pleasure .still if you have some problem you always welcome.

    Thanks
    Rupesh Shukla
    Monday, June 29, 2009 5:32 PM
  • The watch window keeps showing "bad ptr" until I get down to actually using the string arrays. IOW, it shows "bad ptr" when I first declare the string array, while I am iterating through it, and then only when I get to actually using each array of the string does it show that the proper values are actually in the string. Is that normal?
    Lamblion
    Monday, June 29, 2009 6:36 PM
  • Your original requirement can be met using a pointer to a fixed size array.

    char (*p)[MAX_PATH];    // Declare a pointer to a fixed size array
    p = new char[73][MAX_PATH];

    To delete you simply do this once
    delete [] p;


    «_Superman_»
    • Marked as answer by Lamblion Tuesday, June 30, 2009 1:37 PM
    Tuesday, June 30, 2009 7:00 AM
  • Thanks, that's another good example.
    Lamblion
    Tuesday, June 30, 2009 1:37 PM