473,468 Members | 1,294 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

String problems

1 New Member
If you try to "cout" the address of an array of characters you get the array itself.
So how do you get the address if you really want it? (There are a few answers on the web like static casts to <void> and <void>* but they don't seem to work.)
One time we need to know this is when debugging a program which claims we've done an illegal operation.


Another thing

I don't like using the <string> library because I don't like using code I don't understand. Anyway why should Bill Gates have all the fun? So instead of

count = strlen(myString);

I write

char* p =myString;
while(*p) p++;
count=p-mystring;

but this doesn't work if myString has been declared with

const char* mystring = 0;

because the compiler cannot convert const char* to char*. This strikes me as absurd. Like saylng you can't have x=3 because 3 is constant.

So if myString is a const pointer I have to grit my teeth and use strlen and strcpy. (And of course strcpy requires the source to be const.

Grateful for help
May 7 '07 #1
4 1600
emaghero
85 New Member
If you try to "cout" the address of an array of characters you get the array itself.
So how do you get the address if you really want it? (There are a few answers on the web like static casts to <void> and <void>* but they don't seem to work.)
One time we need to know this is when debugging a program which claims we've done an illegal operation.


Another thing

I don't like using the <string> library because I don't like using code I don't understand. Anyway why should Bill Gates have all the fun? So instead of

count = strlen(myString);

I write

char* p =myString;
while(*p) p++;
count=p-mystring;

but this doesn't work if myString has been declared with

const char* mystring = 0;

because the compiler cannot convert const char* to char*. This strikes me as absurd. Like saylng you can't have x=3 because 3 is constant.

So if myString is a const pointer I have to grit my teeth and use strlen and strcpy. (And of course strcpy requires the source to be const.

Grateful for help

If you want to output the address of an array you should use the reference operator &.

Expand|Select|Wrap|Line Numbers
  1. double *ptr;
  2. cout<<"The address of the pointer is "<<&ptr<<endl;
  3.  
Also Bill Gates isn't responsible for the <string> library. That was written long before Microsoft existed. So you're not using anything Microsoft specific if you use it. Just be sure that you adhere to the ANSII standard.

The reason libraries such as <string> exist is because they help programmers to save time when using certain types and functions in C++. The functions contained in such libraries are very well written and are completely general. They are there so that you don't have to reinvent the wheel when it comes to very common functions like strcpy and strlen. If these libraries didn't exist then every programmer would have his/ her own version of strlen, which would not make for very efficient code, especially if you intend using your code on different compilers.
May 7 '07 #2
Strika Amaru
19 New Member
...how do you get the address if you really want it?
Haven't worked with cout for a while, and furthermore i wouldn't recommend it for debugging because it's buffered and if/when a program dies, the output dies with it in the buffer.
Instead, you could use (from <stdio.h>) printf( <string template>, <unlimited number of arguments> ). Sound strange? First time i've seen it, in full detail, i ran back to cout. So I won't go into details :) To see a pointer, the code looks something like this:

Expand|Select|Wrap|Line Numbers
  1. SomeType *ptr;
  2.  // ....
  3. printf( "%d\n", ptr );
"%d\n" is the 'template' of the output; it says, "Print a decimal number and a new line". "ptr" is the number in question. That's about it.

Just thought about this: in cout, do you write *ptr, or ptr? If it's the first, then the poor function is working properly...

char* p =myString;
while(*p) p++;
count=p-mystring;

but this doesn't work if myString has been declared with

const char* mystring = 0;

because the compiler cannot convert const char* to char*. This strikes me as absurd. Like saylng you can't have x=3 because 3 is constant.

So if myString is a const pointer I have to grit my teeth and use strlen and strcpy. (And of course strcpy requires the source to be const.
Several points here:
1. strcpy doesn't *require* the source to be const; const in an argument list is an indication to the compiler; it says "this function doesn't alter this parameter, so it's ok to call it for a constant." If you have a function, say, void foo( char* param ), and you try to pass it a const char*, the compiler will protest; it is forced to maintain the integrity of any const, so it won't accept it being passed to an "unsafe" function.
2. const char* mystring = 0; This one seems bizarre. I just went in gcc and it works without any problems, both with const char* and char*. Are you declaring p as const? Because that's all I can think you doing wrong. const char* p says: "the POINTER is constant". Essentially, you're not assigning x=3, you're assigning 3=4, which is definitely not kosher.

I don't like using the <string> library because I don't like using code I don't understand. Anyway why should Bill Gates have all the fun?
Because library functions are years old, they are definitely correct. And usually they are optimized to the umpteenth level, too...
May 7 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Haven't worked with cout for a while, and furthermore i wouldn't recommend it for debugging because it's buffered and if/when a program dies, the output dies with it in the buffer.
Instead, you could use (from <stdio.h>) printf( <string template>, <unlimited number of arguments> ).
I recommend that you never debug ussiong display statements in your code. Learn to use your debugger. The problem is that those displays need to be taken out, which changes the source code and that not requires a complete new test pass.

Further, in C++ avoid printf() and its lik like vsprintf(), sprint(), etc. They only work woth the built-in types from C. Use an C++ inserter instead.
May 8 '07 #4
AdrianH
1,251 Recognized Expert Top Contributor
I’m sorry to say that there are quite a few errors in this thread, but I don’t have time to go into them all, so I will just answer the question.
If you try to "cout" the address of an array of characters you get the array itself.
So how do you get the address if you really want it? (There are a few answers on the web like static casts to <void> and <void>* but they don't seem to work.)
cout << static_cast<void*>(ptr); is what you are looking for.
One time we need to know this is when debugging a program which claims we've done an illegal operation.


Another thing

I don't like using the <string> library because I don't like using code I don't understand. Anyway why should Bill Gates have all the fun? So instead of

count = strlen(myString);

I write

char* p =myString;
while(*p) p++;
count=p-mystring;
but this doesn't work if myString has been declared with

const char* mystring = 0;
Bill has nothing to do with this. As stated upthread, it is to keep you from having to reinvent the wheel. However, to fix your code you would do this:
Expand|Select|Wrap|Line Numbers
  1. char const * p =myString;  // Problem is on this line (put const in front of char if you wish)
  2. while(*p) p++;
  3. count=p-mystring;
  4.  
This will still allow you to increment the pointer but will also let you assign a const char * to it. Note, this code may not be as efficient as the code provided by the compiler.

As a side note, I put const after the thing I am consting. This is because it is consistent. Putting const in front of the entire expression is the only time that const refers to the type after const.
because the compiler cannot convert const char* to char*. This strikes me as absurd. Like saylng you can't have x=3 because 3 is constant.
No, it’s more like saying 3=x. You cannot assign a constant a new value.
So if myString is a const pointer I have to grit my teeth and use strlen and strcpy. (And of course strcpy requires the source to be const.
No, strcpy() doesn’t require that the source be const, the prototype is saying that when passed, the function strcpy() will not modify the source.

Careful, you grit your teeth any harder and they may shatter. ;)
Grateful for help
Glad to give it.


Adrian

P.S. Using printf is a viable alternative, but you should use either %p (pointer address) or if you want to print as a hex address of a consistent length use %08x and cast the pointer to int. Using %d as stated also works but I find that it isn’t as neat because the length can be very variable. Also, casting is a good idea because there are compilers out there that will check the parameters after the format string against the format string to ensure you are not doing anything wrong.
May 8 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Susan Baker | last post by:
Hi, I have declared a class MyClass in a header file MyClass.h I have then gone onto define the class in MyClass.cpp. This is (roughly) what the definition (.cpp) file looks like: #include...
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
18
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
10
by: zahy[dot]bnaya[At]gmail[dot]com | last post by:
Hi, I am trying to come up with a c style string reverser, I want it to take 1 argument Altough I would never do this in real life. Is there a way to do it? I wrote this function that fails : ...
3
by: sernamar | last post by:
Hi, I have the following base class class UnitOfMeasure { public: //... std::string& GetName() {return _uomName;}; //... protected:
42
by: =?Utf-8?B?UGxheWE=?= | last post by:
I have an if statement that isn't working correctly and I was wondering how I check for a blank string. My Code Example if me.fieldname(arrayIndex) = "" then ----- end if When I do this and...
3
by: ommail | last post by:
Hi I wonder if regular expressions are in general sower than using classes like String and Char when used for validating/parsing text data? I've done some simple test (using IsMatch()) method...
14
by: Javier | last post by:
Hello, in which cases is it better the use of "const char*" to "string" (or even const string &). I mean, in STL (http://www.sgi.com/tech/stl/hash_map.html) I see: hash_map<const char*, int,...
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.