Sunday, September 26, 2010

sizeof operator on a double array in c++

Saturday, September 25, 2010

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

Defining strings in C and C++:
Basically, a string is an array of characters (char) data type variables. And it is defined as following:
char myString[50];
It is possible to initialize the array of characters at run time
char myInitializedString[50]={"I am invincible..."};
An alternate technique of defining character arrays with the help of char pointers can be used as well, which is demonstrated below:

char* myInitializedCharPtr = "Here we go again";

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

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

This 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 arrays
Suppose we need to create a multi-dimensional array on run time, using pointers... We will need a char double pointer or char**, and it will make the code look something like this:










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;
Console will show
c:\>Bst @
Bi! 1
W ared

An alternate way to achieve the same result is given below

cout<<*(greenLandMD+0)<<endl;
cout<<*(greenLandMD+1)<<endl; 
cout<<*(greenLandMD+3)<<endl;At this point, the array will look something like this

There are two things to notice in this image
1- The size of strings contained by array of strings varies.
2- It is possible to leave un-initialized space at any slot of a multi dimensional array in c/c++.
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.

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;

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

I'm working as a freelance programmer through ODesk.com, I have a strong profile over there. Feel free to ping me if you need an Android app for yourself or your employer.

https://www.odesk.com/users/~012d73aa92fad47188