By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,157 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,157 IT Pros & Developers. It's quick & easy.

Strange char pointer error

P: 9
I searched for a similar error, and I didn't find any helpful references, so I'm trying not to post a redundant thread.

I am programming for my summer job using Microsoft Visual C++ 6.0, and a program is having a strange error with char pointers.

The following code worked correctly:
Expand|Select|Wrap|Line Numbers
  1. int testInt = 11;
  2. int* Ptr = &testInt;
  3. cout << *Ptr << endl << Ptr;
  4.  
and gave this output consistently (even the same address):
11
0012F8D0

The following code did not:
Expand|Select|Wrap|Line Numbers
  1. char testChar = 'a';
  2. char* Ptr = &testChar;
  3. cout << *Ptr << " " << Ptr;
  4.  
The output was 'a' followed by 3 lines of strange symbols that I can't type on my keyboard, beyond the first letter, which was 'a'. If I instead typed testChar = 'b', the three lines began with 'b' after printing *Ptr.

I hope this isn't a stupid question, and any help would be greatly appreciated.
Jun 14 '07 #1
Share this Question
Share on Google+
5 Replies


DeMan
100+
P: 1,806
it sounds like the 'cout' is treating both inputs (in your second example) as characters. Assuming the address is the same as in teh previous example (which is not necessarily the case), there are 4 characters to print for the address 0x00, 0x12, 0xf8, 0xD0.
The first is NULL (and is printed differently depending on compiler - but often simply ignored)
The others are treated as ascii charatcers, and none of the above would be within the set of numbers or letters.

You could try casting the pointer to an integer to see it's actual value.....
Jun 14 '07 #2

P: 9
That gave me a reasonable output for the pointer. Thanks.
Jun 15 '07 #3

P: 23
Hi,
I think the reason is char *ptr always points to a string which in C/C++ is always terminated by a NULL(which is a must). So when you are writing like
char* Ptr = &testChar;
it gets "a" in the first position but unluckily not terminated by a NULL. Thats why it goes on printing from memory until it gets a NULL. All these three lines are the output from printing *ptr and I think you MAY get an integer at last which is the output from printing ptr.

You can make it work if you like this :

char testChar = 'a';
char* Ptr = &testChar;
(Ptr+1)= 0; //OR (Ptr+1) ='\0' Thats NULL
cout << *Ptr << " " << Ptr;

Hope it helps.

--Sorower
Jun 15 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
These are not strange errors. They are caused by function overloads of the << operator.

For example, this code:
Expand|Select|Wrap|Line Numbers
  1. int testInt = 11;
  2. int* Ptr = &testInt;
  3. cout << *Ptr << endl << Ptr;
  4.  
calls
ostream& operator<<(ostream&, int) to show 11
then
ostream& operator<<(ostream&, ostream& (*fp)(ostream&)) //to call endl function
then
ostream& operator<<(ostream&, int*) //to show address of int

Whereas this code:
Expand|Select|Wrap|Line Numbers
  1. char testChar = 'a';
  2. char* Ptr = &testChar;
  3. cout << *Ptr << " " << Ptr;
  4.  
calls
ostream& operator<<(ostream&, char) //to show a
then for the " "
ostream& operator<<(ostream&, char*) //which ASSUMES a C-String
then
ostream& operator<<(ostream&, char*) //which ASSUMES a C-String

By my count you have called 5 different functions that do 5 different things but they all look like << to you. This is why function overloading inb C++ is called function polymorphism. It is a form of object-orient programming. Here trhe << operator "does the right thing".

Please desist with the casts. One cast destroys the type safety of your program. You cast in C++ if a) you are calling a relic C function and you have no choice, or b) your C++ design is screwed up.
Jun 15 '07 #5

P: 9
Thanks for the help!
Jun 20 '07 #6

Post your reply

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