![]() |
|
#1
|
|||
|
|||
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
|
||||
|
||||
|
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:
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
|
|||
|
|||
|
Quote:
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:
Regards, Dave |
|
#4
|
|||
|
|||
|
Quote:
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:
Regards, Dave |
|
#5
|
||||
|
||||
|
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 GIDBlog
Last Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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