473,491 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pointer to char

14 New Member
why is it that when a pointer is pointed to an int, it displays the address while when a pointer is pointed to char there is no output, in the following program:
int *pptr;
char *cptr;
cout<<pptr; //o/p shows the hexadecimal address
cout<<cptr; // no o/p, it compiles but the output screen is blank

Please help, this has caused a hindrance in understanding pointers

Please also refer simple tutorials to understand pointers
May 10 '07 #1
2 1057
Savage
1,764 Recognized Expert Top Contributor
why is it that when a pointer is pointed to an int, it displays the address while when a pointer is pointed to char there is no output, in the following program:
int *pptr;
char *cptr;
cout<<pptr; //o/p shows the hexadecimal address
cout<<cptr; // no o/p, it compiles but the output screen is blank

Please help, this has caused a hindrance in understanding pointers

Please also refer simple tutorials to understand pointers
Valid hexadecimal address of char is containing only 0(and x ofcourse),so cout don't print it out.If address of a pointer to char contain value differant then 0 it will print out error:cannot convert int to char*.U can test this by using printf instead.

Savage
May 10 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
The problem is the << operator.

This is really a function call so something like:

Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream& os, char);
  2.  
This function is programmed to display the letter and not the numeric value of the char.

Do this instead:

Expand|Select|Wrap|Line Numbers
  1. char c = 'A';
  2. int temp = c;
  3. cout << temp;
  4.  
Now the << operator is a call to this function:
CODE]
ostream& operator<<(ostream& os, int);
[/code]
This function is programmed to show the numeric value of the int.

Remember, the << operator means FORMATTED OUTPUT. The formatting is accomplished by an appropriate overload of the << operator.

Avoid printf(). This function cannot be used to output to anythign other than the screen. AND is only works with the buiklt-in types of C. However, a << function will output to whereever the os object says to. That means without changing your code you can output to the screen, the disc, or maybe even send a packet across a network.
May 10 '07 #3

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

Similar topics

10
2034
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << "...
4
16445
by: Bryan Parkoff | last post by:
I want to allocate pointer array into memory so pointer array contains ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks like below for example. unsigned char* A = new...
35
2852
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
2261
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
12882
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
5
5328
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: ...
15
3720
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
5
1651
by: mdh | last post by:
Hi all, I have gone through the FAQ and done searches in the Comp.Lang and if I have missed it please let me have the ref. (Question generated in part by p119). Given char a;
10
3411
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
8
1833
by: tfelb | last post by:
Hey group! I have 2 questions. I saw functions with char *dst = (char *)src. In that case if I remember what I've learned I assign (an) (the) address of src to dst. Right? But I can assign...
0
7154
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
7190
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...
1
6858
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
4881
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
4578
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...
0
3086
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
1392
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 ...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
280
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...

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.