Saturday, September 25, 2010

Char** Array of Strings in C and C Plus Plus(C++)



char** greenLandMD;
greenLandMD = new char*[5];// initialize the double pointer
greenLandMD[0]=new char[4];// initialize 1st char*, with capacity of 4 chars
greenLandMD[1]=new char[5];// initialize 2nd char*, with capacity of 5 chars
    
strcpy(greenLandMD[0],"Bst @");//copy some data to 1st string
strcpy(greenLandMD[1],"Bi! 1");//copy some data to 2nd string
greenLandMD[3]="W ared";// an alternate way of copying data
 
// print and see the data
cout<<greenLandMD[0]<<endl<<greenLandMD[1]<<endl<<greenLandMD[3]<<endl;
The source code given above is an example usage of char**. Keep reading to learn more & see detailed examples.

Char** Strings Array C C++

Defining strings in C and C++:
Basically, a string is an array of characters (char) data type variables in C and C++ programming languages. An array of characters is defined as following:
char myString[50];

Note: Do check my C# online programming courses with discount below



It is possible to initialize the array of characters at run time with below line of code
char myInitializedString[50]={"I am invincible..."};
Learn C Programming Language Multiple Colors



An alternate technique of defining character arrays with the help of char pointers can be used as well(char* stands for character pointer), which is demonstrated below:

char* myInitializedCharPtr = "Here we go again";



Multi dimensional arrays in C and c++ programming languages:
What if someone wants to store multiple strings in an array? It's called array of string, or multi-dimensional array in other words. Just recently someone asked me, how to store multiple strings in array in C programming language? It is fairly simple to create and initialize an array of strings in your program

char ArrayOfStringsInitialized[3][50] = {"Rock and Rolla","iToka","Thats it"};



This multidimensional array of strings contains 3 strings, each of length up to 50 characters.
To iterate the array we just created, do the following...

for(int n = 0; n<=2/*less than last element of array*/;n++)
{
        cout << ArrayOfStringsInitialized[n] << endl;
}
For two dimensional array defined with this technique, the programmers don't need to free the memory explicitly.

Dynamic two dimensional (2D) array
Suppose we need to create a multi-dimensional array on run time, using pointer types... We will need a char double pointer or char**, two stars stand for double pointer, and it will make your C++ programming language code look something like the example given below:










char** greenLandMD;
greenLandMD = new char*[5];// initialize the double pointer
greenLandMD[0]=new char[4];// initialize 1st char*, with capacity of 4 chars
greenLandMD[1]=new char[5];// initialize 2nd char*, with capacity of 5 chars
    
strcpy(greenLandMD[0],"Bst @");//copy some data to 1st string
strcpy(greenLandMD[1],"Bi! 1");//copy some data to 2nd string
greenLandMD[3]="W ared";// an alternate way of copying data
 
// print and see the data
cout<<greenLandMD[0]<<endl<<greenLandMD[1]<<endl<<greenLandMD[3]<<endl;

When you will run your C++ program, the console will show output as following: 

c:\>Bst @
Bi! 1
W ared

An alternate way to achieve the same result in C++ language is shown in example source code given below

cout<<*(greenLandMD+0)<<endl;
cout<<*(greenLandMD+1)<<endl; 
cout<<*(greenLandMD+3)<<endl;

At this point, the array will look something like the graphic shown below. 

There are two things to notice in this image

  1. The size of strings contained by array of string is not same, the length is varying.
  2. It is possible to leave un-initialized space at any slot of a multi dimensional array in C/C++ programs.

It is a programmer's responsibility to free the memory acquired using this technique, missing such things create memory leaks which cause performance decrease, security related problems, and an all out crash some times. Debugging subtle semantic error like these are often a programmer's worst nightmare.

Modern virtual machine based programming languages such as Java and C# solve memory allocation and cleanup problems on behalf of the programmers. There is a performance penalty associated when the JVM or CLR is managing the memory on your behalf. But, such programs are way more safe as compared to bare bones C/C++ programs where you have to do the memory allocation & cleanup yourself. But there still are problems which must be solved using unmanaged C/C++ code, one such example is software used in fighter jets. Lockheed Martin recently won a F-35 software upgrade bid worth $1.8 Billion. I believe this software upgrade must be done in assembly, C, C++, or another such language.

Freeing memory of char * and char**
Deleting the main array will suffice to free the memory consumed by double pointers. In our case, we'll do following:

delete [] greenLandMD;

The C programming language uses malloc and memfree for allocating and freeing up memory. There was another method, memset, it was used to cleanup memory allocated to a program. But that's beyond the scope of this article. 

I think this much is enough on this topic, for more please feel free to comment this post.


More than 65% discount on my online courses can be availed now by clicking the links below: 



9 comments:

  1. Should we have a '\0' character?

    ReplyDelete
    Replies
    1. A Programmer'S Day: Char** Array Of Strings In C And C Plus Plus(C++) >>>>> Download Now

      >>>>> Download Full

      A Programmer'S Day: Char** Array Of Strings In C And C Plus Plus(C++) >>>>> Download LINK

      >>>>> Download Now

      A Programmer'S Day: Char** Array Of Strings In C And C Plus Plus(C++) >>>>> Download Full

      >>>>> Download LINK EA

      Delete
  2. Lê Trường An: When we get memory for a char * using new keyword, it is good to clean it using "memset" method call, and supply 0 as second character.
    For more information, please see:

    http://www.cplusplus.com/reference/clibrary/cstring/memset/

    ReplyDelete
  3. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from

    ReplyDelete
  4. Join video editing course online Masters certification program from around the world already studying on Subhe. You will learn how to work on tools like Adobe Premiere Pro, Adobe After Effects, Final Cut Pro and Adobe Photoshop. Study from the best academicians & top industry experts, making you corporate ready. Start today and develop your skills and you will have an internship even before your training ends.
    Also, check out our other courses:
    Digital Marketing Course
    Photoshop tutorials in hindi
    masters in UI/UX design in India

    ReplyDelete
  5. https://www.limoni.ch/?attachment_id=775#comment-247556

    ReplyDelete
  6. A Programmer'S Day: Char** Array Of Strings In C And C Plus Plus(C++) >>>>> Download Now

    >>>>> Download Full

    A Programmer'S Day: Char** Array Of Strings In C And C Plus Plus(C++) >>>>> Download LINK

    >>>>> Download Now

    A Programmer'S Day: Char** Array Of Strings In C And C Plus Plus(C++) >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete
  7. your article so amazing and informative. Your writing skill is also very well. Your article is really addictive. Keep posting. keep sharing the knowledge. I love to read your articles.
    english stories english short stories with moral value What is the factorial of 100

    ReplyDelete

Feel free to talk back...