Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to print address of a string.

Status
Not open for further replies.

96gfeeinte

Technical User
Jun 23, 2003
5
0
0
IN
hi friends,
i hav just started learning c++.i hav a pgm in C.but i don't know how to do the same in c++.so, can anyone help me out??????
the pgm in C is:

main () {
char s[10] = "GOOD";

printf ("%s, %u, %u\n", s, s, "GOOD");
}
o/p:
GOOD, 1324568(addres.. not exact ans), 1324568(same address).

now, how do i get same type of o/p in c++.


 
I can't imagine that you got that output (hint: it doesn't help to intersperse your comments like that).

You should use the %p specifier instead of %u when printing pointer addresses with printf. You should also cast each of the pointer values to pointer to void, since %p requires a pointer to void and the automatic conversions that occur with regular function calls don't happen with variadic functions like printf.

char s[10] = "GOOD";

printf ("%s, %p, %p\n", s, (void *)s, (void *)"GOOD");

And you're already there... You're programming in the common subset of C and C++, so this is valid C++ code.
 
fee,
You have been suggested appropriately. C++ prefers usage of string class. you can do something like this:

string s = "good";
cout << &s << endl;

hope this helps
 
Slicker solution, no?

#include <iostream>
#include <string>
void process( void )
{
char c[] = {&quot;GOOD&quot;};
std::string str = c;
std::cout << '\&quot;' << str << &quot;\&quot; 0x&quot; << &str << &quot; 0x&quot; << &c << std::endl;
printf (&quot;\&quot;%s\&quot; 0x%p\n&quot;, c, (void *)c );
}
int main( int argc, char* argv[] )
{
const int result = 0;
process();
return result;
}
 
#include<iostream>
using namespace std;
int main ()
{
char s[10] = &quot;GOOD&quot;;
cout<< &quot;address or string s{&quot;<< s &quot;} is &quot;<< (int)s<< endl;
return 0;
}

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Hmm. I'm inviting myself to discuss the previous post, which appears as a response to an earlier post of mine. I have to infer a little, since there are no comments.

I think that there are three undesirable aspects to the adjustments that this last post made (I actually made my post to correct these):

1. The first one is just bad programming:

Code:
char s[10] = &quot;GOOD&quot;;

versus, my:

Code:
 char c[] = {&quot;GOOD&quot;};

Why not just let the compiler figure out the size requirement, rather than this strange value &quot;10&quot;, which will break when some one comes along and changes &quot;GOOD&quot; to &quot;NOT SO TERRIBLY GOOD&quot;?

2. Very few people appreciate addresses in decimal, they don't mean too much. Hex is more usable. It is a convention to specify hex as e.g. &quot;0x10&quot; (decimal 16), hence my change with the &quot;0x&quot;.

3. Namespace usage.

While it is less typing to say &quot;using namespace std;&quot; and then you avoid the need to specify &quot;std::&quot; before standard library references, this approach actually defeats the whole point of namespaces. Namespaces were created to avoid naming collisions between different compilation units. I might, for example, create a variable &quot;cout&quot; for whatever reason (I know, not a good example). I can't do this if there is a &quot;using namespace std;&quot;. But I would be fine with &quot;cout&quot; & &quot;std::cout&quot; coexisting.
 
A slightly different answer:

If you're looking for a way to output the address of a char* via a C++ ostream such as cout, just cast the char* to a void* to keep the ostream from treating it specially.
 
>melancthon
as far as I see, question is about adderss of a string, not about how to use STL

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
No disrespect, but I can't understand your comment. Are you referring to the use or abuse of namespaces?
 
>melancthon
I did not talk about namespace at all. In that small program I have posted is no difference. My answer was on how to print the address of a string.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top