Sunday, September 26, 2010
Saturday, September 25, 2010
Char** Array of Strings in C and C Plus Plus(C++)
The C++ source code shown below is an example of char**, also known as a character double pointer, which is used for managing two-dimensional arrays of string variables.
Keep reading to learn more & see detailed examples.
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;
Defining strings in C and C++:
A string is an array of characters (char) data type variables in C and C++ programming languages. For example, an array of characters is defined as follows:
char myString[50];
Note: Do check my C# online programming courses with a discount below
Best-selling Udemy course
It is possible to define a character array and initialize it on the fly with the code below. That's how we can doll two birds with one stone!
char myInitializedString[50]={"I am invincible..."};
An alternate technique of defining C-style 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";
What if someone wants to store multiple strings in an array? It's called an array of strings or a multi-dimensional array. Someone recently asked me how to keep numerous strings in an array in the C programming language? It is relatively simple to create and initialize an array of strings in your program.
char ArrayOfStringsInitialized[3][50] = {"Rock and Rolla","iToka","Thats it"};
This multi-dimensional 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; }
Notice that you got 50 characters allocated but used only 14 for "Rock and Rolla" so if you try to get the length of the string in this case it will be 14, the space occupied by the characters of the strings you just stored in the array. It won't be 50 because the string termination character \0 would've been found much earlier.
Dynamic two-dimensional (2D) array:
If you want to create a multi-dimensional array on run time, using pointer types. You will need a char double pointer or char**. The two stars stand for a 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 run your C++ program, the console will show output as follows:
c:\>Bst @ Bi! 1 W are |
The example source code below shows an alternate way to achieve the same result in C++.
cout<<*(greenLandMD+3)<<endl;
At this point, the array will look like the graphic below.
cout<<*(greenLandMD+0)<<endl; cout<<*(greenLandMD+1)<<endl;
At this point, the array will look like the graphic below.
- The size of strings contained by an array of strings is not the same. The length of strings can be variable.
- It is possible to leave un-initialized space at any slot of a multi-dimensional array in C/C++ programs. There will be software security implications but that's not beyond the scope of our piece.
Modern virtual machine-based programming languages such as Java and C# solve memory allocation and cleanup problems on behalf of the programmers. Of course, there is a performance penalty associated when the JVM or CLR is managing the memory on your behalf. But, such programs are way safer than bare-bones C/C++ programs where you have to do the memory allocation & cleanup yourself. But there still are problems that must be solved using unmanaged C/C++ code; one example is fighter jet software. Lockheed Martin recently won an F-35 software upgrade bid worth $1.8 Billion. I believe this software upgrade must be done in assembly, C, C++, or another 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 the following:delete [] greenLandMD;
The C programming language uses malloc and memfree for allocating and freeing up memory. There was another method, memset, which was used to clean up memory allocated to a program. But that's beyond the scope of this article.
I think this is enough on this topic; please feel free to comment on this post if you want to learn more.
As of 2024, this coding example/tutorial is still valid and useful. I've read somewhere that Rust programming language is going to take the place of C and C++ in the future. We'll see when that happens.
More than 65% discount on my online courses can be availed now by clicking the links below:
Subscribe to:
Posts (Atom)