473,411 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Strange char pointer error

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
5 1717
DeMan
1,806 1GB
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
That gave me a reasonable output for the pointer. Thanks.
Jun 15 '07 #3
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
9,208 Expert Mod 8TB
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
Thanks for the help!
Jun 20 '07 #6

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

Similar topics

3
by: cylin | last post by:
Dear all, I met a char pointer problem. please help,thanks. How to change the the value which is pointed by a char pointer by a function? for example,...
13
by: chellappa | last post by:
hi , please explain me , char pointer , char Array, char ,string... i have some many confussion about this... please clarify to me.. i need some example about this program by chellappa.ns
8
by: mailursubbu | last post by:
Hi, Will it be possible to #define a char pointer... It means if write some thing like #define CHAR_PTR (char *) // I know this wont work I should be able to use CHAR_PTR to define a...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
5
by: ryanlee101 | last post by:
I am getting a exception error when I complie my code. The error is: - imageData 0x00000000 <Bad Ptr> type unsigned char * I think it is from when I declare some of my char variables
17
by: matevzb | last post by:
I've ran into some fishy code that, at first glance, is buggy, but it seems to work correctly and none of the compilers I've tried (five so far, on various systems) gives any warnings. The code:...
3
by: C++Liliput | last post by:
I have a class of the type class A { private: std::string data; ....... public: const char* toString(); };
6
by: comp.lang.c++ | last post by:
this is a sample example about this question #include<stdio.h> void chg(char* t) { char *s=t; char p=*t; while(*t++=*++s); *--t=p; } int main()
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.