Saturday, September 25, 2010

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


The C++ source code given below is an example usage of char**. It is also called a character double pointer, 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;


Char** Strings Array C C++

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


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 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;
}
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**. 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+0)<<endl;
cout<<*(greenLandMD+1)<<endl; 
cout<<*(greenLandMD+3)<<endl;

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

There are two things to notice in this image.

  1. The size of strings contained by an array of string is not the same. The length varies.
  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 sometimes. 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. 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 much is enough on this topic; for more, please feel free to comment on this post.


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



Friday, January 15, 2010

Path to Microsoft Certification for programmers

Path-To-Microsoft-Certification-MCTS
In university days I once tried to follow path to Sun Java Certified Programmer. On their web site I found very nice flow charts. For Microsoft's certification I could not find anything of this sort therefore I decided to collect data from Microsoft's web and create my own Microsoft Certification Tree diagram.










Everything seems to be starting from Exam 70-536: TS: Microsoft .Net Framework, Application Development Foundation. We will get MCTS after passing two exams, and MCPD after passing third exam of next stage.
Update: Everything still starts from Exam 70-536.

History of vendor certifications

Once upon a time these certification were the thing of the day. Small institutes would show sweet dreams to jobless young people and tell them that an MCSE can get them a job, or a visa to the USA.
The certifications became pretty useless with the introduction and popularity of dums, people would just spend a couple days learning the dumps by rote and pass certification exams with excellent score.

The certifications were not made to withstand such abuse(but Indians you know rape anything feminine they come across, or at least they try to). Various international bodies shut their certification and examination tracks in many countries. Comptia A+ for example, was a hardware professional certification which was sacked despite its popularity from many developing countries.

Certifications are totally useless?

The short answer is No. If someone prepares well for a certification and studies well; passing a certification will result in significant increase in knowledge about specific technology(ies).
There are still several great certifications out there by top companies like Cisco, Microsoft, Oracle, Zend.
Technology certifications are not an alternative to proper bachelors degree, they augment your graduate studies instead. Just like FCPS is not an alternative to MBBS, it is an additional qualification that makes medical professionals better in their job.