GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Mar-2005, 18:34
mirizar mirizar is offline
New Member
 
Join Date: Jun 2004
Posts: 20
mirizar is on a distinguished road

Are arrays of character pointers treated specially?


Hi!

I have created an array of character pointers
char *myArray[2] = {"test", "test2"};

If I print myArray[0] it prints "test" instead of the address of the test string.

Is there any way to print the address where "test" is stored instead of the string "test"?

Any help would be greatly appreciated.

Thanks, Michelle
  #2  
Old 03-Mar-2005, 00:48
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Pointers to char can be really tricky in C/C++.
I wrote a simple test that you can run and figure it all out, hopefully. Please note that I wouldn't normally write a program in this manner; what I mean is if I saw a piece of code written like this by anybody else I would probably flip

So, this is just a test:

CPP / C++ / C Code:
	int a = 5, b = 10;
	char * myPointerTOCharArray[2] = {"test1", "test2"};
	int * myPointerTOIntArray[2] = {&a, &b};;
	int myIntArray[2] = {2, 4};
	
	//This is tricky :)
	char * test3 = "test3";
	char * test4 = "test4";
	char ** myPointerTOPointerToCharArray[2] = {&test3, &test4};

	cout << "The first element's value" << endl;

	cout << myPointerTOCharArray[0] << endl;
	cout << myIntArray[0] << endl;
	cout << * myPointerTOIntArray[0] << endl;
	cout << * myPointerTOPointerToCharArray[0]	<< endl;
	
	cout << "\nThe first element's address" << endl;

	cout << & myPointerTOCharArray[0] << endl;
	cout << & myIntArray[0] << endl;
	cout << myPointerTOIntArray[0] << endl;
	cout << myPointerTOPointerToCharArray[0] << endl;

If you use a C compiler, you'll have to change the printing.

Anyway, the quick answer to your question would be to print & myArray[0] to get the address. I hope this is fine.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 03-Mar-2005, 07:52
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by mirizar
Hi!

I have created an array of character pointers
char *myArray[2] = {"test", "test2"};

If I print myArray[0] it prints "test" instead of the address of the test string.

Is there any way to print the address where "test" is stored instead of the string "test"?

Any help would be greatly appreciated.

Thanks, Michelle

cout is an example of an ostream. The extraction operator, <<, treats pointer to char as a c-style string (a sequence of chars terminated by a zero byte).

If you want to see the address of a pointer, cast it to "pointer to void".

You can play around with this:
CPP / C++ / C Code:
#include <iostream>
using std::cout;
using std::endl;

int main()
{
  char *messages[2] = {"First Message", "Second Message"};
  char *pChar;

  pChar = messages[1];

  cout << "Here's cout << pChar:         <" << pChar << ">" << endl << endl;
  cout << "Value of the pointer pChar   = " << (void *)pChar << endl;
  cout << "Address of the pointer pChar = " << &pChar << endl << endl;

  for (int i = 0; i < 2; i++) {
    cout << "messages[" << i << "]: <" << messages[i] << ">" << endl;

    cout << "Value of pointer messages[" << i 
         << "] = " << (void *)messages[i] << endl;

    cout << "Address of messages[" << i << "]       = " 
         << &messages[i] << endl << endl;

  }


  return 0;
}

Regards,

Dave
  #4  
Old 03-Mar-2005, 08:23
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by davekw7x
cout is an example of an ostream. The extraction operator, <<, treats pointer to char as a c-style string (a sequence of chars terminated by a zero byte).

If you want to see the address of a pointer, cast it to "pointer to void".

You can play around with this:

I am responding to my own response (again).

I gave the "C++" approach (that is, I used cout, as the Original Poster did). Actually, real C++ programmers would not use the static cast (void *), but it's perfectly valid in C++, and is, in my opinion, appropriate for this simple case.

As a further note, I would like to note that people who have learned C before getting into C++ sometimes yearn for good old printf(), and really hate to learn all the ins and outs of cin>> and cout << (a little C++ pun here, folks).

Well, the good news is that printf() and all of its relatives are part of the standard C++ library also, and its really (really) OK to use them in C++. Why printf()? Well printf supports a format specifier that prints values of pointers without requiring casts. You can try this for comparison:
CPP / C++ / C Code:
#include <iostream>
#include <cstdio>

using std::cout;
using std::endl;
using std::printf;

int main()
{
  char *messages[2] = {"First Message", "Second Message"};
  char *pChar;

  pChar = messages[1];

  printf("Using printf pChar with %%s:   <%s>\n", pChar);
  printf("Using printf pChar with %%p:    %p\n", pChar);
  printf("Address of   pChar        :    %p\n\n", &pChar);
  cout << "Here's cout << pChar:         <" << pChar << ">" << endl;
  cout << "Value of the pointer pChar   = " << (void *)pChar << endl;
  cout << "Address of the pointer pChar = " << &pChar << endl << endl;

  return 0;
}


Regards,

Dave
  #5  
Old 03-Mar-2005, 08:51
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Ooops!

Now that I read Dave's posts I understand the error in mine. Disregard it please, his solution is the correct one.

Regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2D arrays:dynamic allocation and freeing bravetanveer C Programming Language 48 27-Nov-2007 15:55
Re: Conversion: Binary, Decimal, Hexadecimal WaltP C Programming Language 1 10-May-2006 06:49
Using character pointers to store the person's name internethesabi C Programming Language 9 12-Mar-2004 15:44
pointers and arrays jack C Programming Language 4 15-Jan-2004 12:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:47.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.