Wednesday, September 1, 2010

Arrays of Arrays

Array of C-strings

An array of C-strings is an array of arrays. Here is an example.
char* days[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};
In the above days array each element is a pointer to a string, and they don't have to be the same size. For example,
char * greetings[] = {"Hello", "Goodbye", "See you later"};

Parameters to main

When the main function is called, it is passed the name of the program that is being run and all the tokens (words) on the remainder of the line that was used to run the program. These are passed to the main as an array of C-strings and the number of elements in the array. Here is a main program that prints these parameters.
using namespace std;

int main(int argc, char* argv[]) {
for (int i=0; i
cout << argv[i] << endl;
return 0;

Array of pointers or pointer to pointer

Because arrays and pointers are so interchangeable, you can write the header of main like this also:
int main(int argc, char* argv[]) { ...

No comments:

Post a Comment