Pointers of array with (char)

#include <iostream>

using namespace std;

int main()
{
   char myStr[] = "text";
   myStr[0] = 'n';
   cout << myStr << endl;

   return 0;
}

Pointer of array

int* pInt = new int(5);
cout <<  pInt << endl; // -> 00385B70
cout << *pInt << endl; // -> 5
cout << &pInt << endl; // -> 0012FF60

char* pChar = new char('t');
cout <<  pChar << endl; // -> t####|||§ (one 't' followed by rubbish)
cout << *pChar << endl; // -> t
cout << &pChar << endl; // -> 0012FF54

char* pStr = "text";
cout <<  pStr << endl; // -> text
cout << *pStr << endl; // -> t 
cout << &pStr << endl; // -> 0012FF48 

Particularly printing the value of the variable yields different output depending on if it's a pointer-to-int or if it's a pointer-to-char. pInt contains the address of the int it's pointing to which is how pointer variables are supposed to work. pChar and pStr do not contain addresses (if you print the values using cout). Instead they contain character literals and cout print them as a string.
Is there some logic in this behavior? I mean, why should the value of a pointer-to-char variable be treated in any other way than the value of a pointer-to-int or a pointer-to-float?

An option would be to type-cast it to some other pointer type for which "cout<<" shows the address:
 
cout << (void*) mycharpointer;

C++ array of pointers

Before we understand the concept of array of pointers, let us consider the following example, which makes use of an array of 3 integers:
#include <iostream>
 
using namespace std;
const int MAX = 3;
 
int main ()
{
   int  var[MAX] = {10, 100, 200};
 
   for (int i = 0; i < MAX; i++)
   {
      cout << "Value of var[" << i << "] = ";
      cout << var[i] << endl;
   }
   return 0;
}
When the above code is compiled and executed, it produces the following result:
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
There may be a situation, when we want to maintain an array, which can store pointers to an int or char or any other data type available. Following is the declaration of an array of pointers to an integer:
int *ptr[MAX];
This declares ptr as an array of MAX integer pointers. Thus, each element in ptr, now holds a pointer to an int value. Following example makes use of three integers which will be stored in an array of pointers as follows:
#include <iostream>
 
using namespace std;
const int MAX = 3;
 
int main ()
{
   int  var[MAX] = {10, 100, 200};
   int *ptr[MAX];
 
   for (int i = 0; i < MAX; i++)
   {
      ptr[i] = &var[i]; // assign the address of integer.
   }
   for (int i = 0; i < MAX; i++)
   {
      cout << "Value of var[" << i << "] = ";
      cout << *ptr[i] << endl;
   }
   return 0;
}
When the above code is compiled and executed, it produces the following result:
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
You can also use an array of pointers to character to store a list of strings as follows:
#include <iostream>
 
using namespace std;
const int MAX = 4;
 
int main ()
{
   char *names[MAX] = {
                   "Zara Ali",
                   "Hina Ali",
                   "Nuha Ali",
                   "Sara Ali",
   };

   for (int i = 0; i < MAX; i++)
   {
      cout << "Value of names[" << i << "] = ";
      cout << names[i] << endl;
   }
   return 0;
}
When the above code is compiled and executed, it produces the following result:
Value of names[0] = Zara Ali
Value of names[1] = Hina Ali
Value of names[2] = Nuha Ali
Value of names[3] = Sara Ali

Share on Google Plus

About Engr. Rokon Khan

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment