473,549 Members | 2,982 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cout fails to print the address of a null char pointer?

Hello,

I am learning C++ lately. This morning, out of curiosity I
constructed a simple code below, but to my surprise, when char* is
used, the program fails to print almost anything.
The only thing I got is the following:

% [0]: np =

As soon as I use a pointer to another type, everything behaves as
anticipated. This is a really trivial code, but I am completely
puzzled. Any hints are appreciated.

Compiler: gcc version 3.4.2
Environment: a Dell 1450 laptop running SUN Solaris 10 intel

Thanks,

--Zack
---------------------------------------- The test code
------------------------------------------
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int main(void)
{
// if string* is changed to char*, then the program no longer
prints
//string* np = 0;
//string** pnp = &np;
// should figure out the reason w
char* np = 0;
char** pnp = &np;
cout << "[0]: np = " << np << endl;
cout << "[1]: pnp = " << pnp << endl;
cout << "Why the above statements not printing?" << endl;
return 0;
}
Aug 5 '08 #1
1 4265
On Aug 5, 1:22*pm, zackp <zack.pe...@sbc global.netwrote :
* * // if string* is changed to char*, then the program no longer
prints
* * //string* np = 0;
* * //string** pnp = &np;
* * // should figure out the reason w
* * char* np = 0;
* * char** pnp = &np;
* * cout << "[0]: np = " << np << endl;
operator<< on ostream is overloaded for char* type and will follow the
pointer to print the characters in that C-style string. You must not
pass use the null pointer in that case.

If you want to print the pointer value, then cast it to void* first
(which is 0 in this case):

cout << "[0]: np = " << static_cast<voi d*>(np) << endl;
* * cout << "[1]: pnp = " << pnp << endl;
No problem with that one, because it is the address of np and is not
NULL.

Ali
Aug 5 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3095
by: nish.parikh | last post by:
Hi, I am using std::cout to print a char pointer that is NULL. Subsequent calls to std::cout dont print anything. Is this the expected behavior? example: #include <iostream> int main( int argc, char *argv ) { char *q = 0; std::cout << q << "\n";
15
2220
by: dandelion | last post by:
Hi, Just another question for the standards jockeys... Suppose I have an Interrupt Vector Table located at address 0x0000 (16-bit machine). I want to dump the context of the IVT, by treating it as an array starting at (you guessed it) 0x0000. So I would have struct iv_s* ivt = (struct iv_s *) 0x0000;
29
22539
by: JS | last post by:
I would like to print char 'd': main(){ char g; g = 'a'; g = 'b'; g = 'c'; g = 'd';
8
7556
by: vijay | last post by:
Hello, As the subject suggests, I need to print the string in the reverse order. I made the following program: # include<stdio.h> struct llnode { char *info;
14
9318
by: Ralf Goertz | last post by:
Hi, this might be intentional, but I don't see a reason, why. Running the program ---------------------- #include <iostream> using namespace std; int main(){
14
3173
by: Vlad Dogaru | last post by:
Hello, I am trying to learn C, especially pointers. The following code attempts to count the appearences of each word in a text file, but fails invariably with Segmentation Fault. Please help me out, I've already tried all my ideas. Also, please do comment on my coding style or other aspects. Thank you. #include <stdio.h> #include...
19
33322
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can show the number:
3
1867
by: subramanian100in | last post by:
Consider the code: #include <iostream> using namespace std; int main( ) { cout << "test string "; cout.operator<<(10).operator<<(endl);
11
1245
by: nembo kid | last post by:
My homework is to make a simple function that, given a unsigned char, should returns its bit string representation. I tried to drop down some code (as follows) but it doesn't work. Compiling is successfully, but print8bit() doesn't return anything. Was wondering where I'm mistaking. Thanks in advance for your help. /* code starts here */
0
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7808
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5087
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.