473,856 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does address-of-char give me non-terminated c-string gibberish?

Hi all,

This isn't minimal code, but at least it gives the idea reasonably well
- and yes, I'm a newb :(

The point of me giving this code is that all three vars look to my eye
as though they're being treated in exactly the same way, so that I end
up confused about why the address-of-char doesn't get returned the way
the other address-of-types do.

(Sorry for bad formatting - I copy/pasted straight out of my IDE...)

Lil insight?

Thanks!

==============
#include <iostream>

using namespace std;

void main()
{
int a = 1;
char b = 'q';
double c = 1.3456;

cout << "Type Size Value Address" << endl;

cout << "int" << " " << sizeof(a) << " " << a << "
" << &a << endl;
cout << "char" << " " << sizeof(b) << " " << b << "
" << &b << endl;

//Why does the above line give gibberish for the address-of-char?

//It's as though it's related to the issue of a c-style string being
an array of char + a null
//on the end, and if you forget to put the null when making a string
"manually", and
//try to print it out, you end up with every memory location being
printed until it
//happens to run across a null. But I really have no idea what the
deal here is.

//Note that I get the expected result if I use the following to begin
with, and put in the obvious
//changes later on:
//char* b = new char('q'); (etc...)

cout << "double" << " " << sizeof(c) << " " << c << " "
<< &c << endl;

return;
}

Oct 25 '05 #1
10 2053
On 2005-10-25, sh************* @gmail.com <sh************ *@gmail.com> wrote:
Hi all,

This isn't minimal code, but at least it gives the idea
reasonably well - and yes, I'm a newb :(

The point of me giving this code is that all three vars look to
my eye as though they're being treated in exactly the same way,
so that I end up confused about why the address-of-char doesn't
get returned the way the other address-of-types do.

(Sorry for bad formatting - I copy/pasted straight out of my
IDE...)
Check if your IDE has an option to convert tabs to spaces. That
will correct any problems when posting the code.

If not, consider writing your own converter, as an exercise. #include <iostream>

using namespace std;

void main()
main returns an int.
{
int a = 1;
char b = 'q';
double c = 1.3456;

cout << "Type Size Value Address" << endl;

cout << "int" << " " << sizeof(a) << " " << a << "
" << &a << endl;
cout << "char" << " " << sizeof(b) << " " << b << "
" << &b << endl;

//Why does the above line give gibberish for the address-of-char?
//It's as though it's related to the issue of a c-style string being
an array of char + a null
That's right. The standard ostream thinks a pointer to char is a
C-string. Cast your pointers to void* to output them.

cout << "int" << " " << sizeof(a) << " "
<< a << " " << static_cast<voi d*>(&a) << endl;
cout << "char" << " " << sizeof(b) << " "
<< b << " " << static_cast<voi d*>(&b) << endl;

As an aside, you should look into the io manipulators. They may make
building your little table simpler.
return;
return 0;
}


--
Neil Cerutti
Oct 25 '05 #2
"Neil Cerutti" <le*******@emai l.com> wrote in message
news:sl******** **************@ FIAD06.norwich. edu
On 2005-10-25, sh************* @gmail.com <sh************ *@gmail.com>
wrote:

(Sorry for bad formatting - I copy/pasted straight out of my
IDE...)


Check if your IDE has an option to convert tabs to spaces. That
will correct any problems when posting the code.


You'd think so wouldn't you. As it happens, it is not true if pasting
directly from the VC++ IDE into Outlook Express. Observe below how "spaces
only" code comes out (the two lines inside main are both indented in the
VC++ IDE with 4 spaces):

int main()
{
int x = 1;
return 0;
}

If you follow a more indirect route: VC++ to Notepad to Outlook Express,
then it comes out OK:

int main()
{
int x = 1;
return 0;
}

--
John Carson

Oct 25 '05 #3
On 2005-10-25, John Carson <jc************ ****@netspace.n et.au> wrote:
"Neil Cerutti" <le*******@emai l.com> wrote in message
news:sl******** **************@ FIAD06.norwich. edu
On 2005-10-25, sh************* @gmail.com <sh************ *@gmail.com>
wrote:

(Sorry for bad formatting - I copy/pasted straight out of my
IDE...)


Check if your IDE has an option to convert tabs to spaces. That
will correct any problems when posting the code.


You'd think so wouldn't you. As it happens, it is not true if pasting
directly from the VC++ IDE into Outlook Express. Observe below how "spaces
only" code comes out (the two lines inside main are both indented in the
VC++ IDE with 4 spaces):

int main()
{
int x = 1;
return 0;
}


Yikes!
--
Neil Cerutti
Oct 25 '05 #4
re: tab/space/xfer-code-from-IDE - all good - I'm familiar with the
origin-notepad-desitination cycle - will do next time

re: \t (I assume that's one of the things you're referring to by "io
manipulators") - a fine idea!

re: main() returning something - what's wrong with void? (Not to argue,
just curious)

re: My original issue: awesome, thanks! (awesome because that means
that I wasn't *just* being stupid :)
Thanks again!

cdj

Oct 25 '05 #5
Ian
sh************* @gmail.com wrote:

re: main() returning something - what's wrong with void? (Not to argue,
just curious)

It's non-standard.

If you application is being run from a script or environment that wants
to know if the application succeeded or failed....

Ian
Oct 25 '05 #6
sh************* @gmail.com wrote:
re: \t (I assume that's one of the things you're referring to by "io
manipulators") - a fine idea!

<snip>

No, "I/O manipulators" are things such as those declared in <iomanip>,
like std::setw() to set the minimum width of an output field,
std::setprecisi on() to control how many digits will get printed, etc.

--
Marcus Kwok
Oct 25 '05 #7
Ah. Well then a fine idea that I'm too newbish to take advantage of at
the moment. I.e., I have no idea what you're talking about - lol

Lemme look in Prata's C++ Primer real quick.... Ok, that stuff doesn't
seem to be addressed until the last chapter of the book.... I'll take a
look at the material, and will happily make use of that which I'm able
to make sense of at this stage.

Thanks!

cdj

Oct 25 '05 #8
Ian wrote:
sh************* @gmail.com wrote:

re: main() returning something - what's wrong with void? (Not to argue,
just curious)

It's non-standard.

If you application is being run from a script or environment that
wants to know if the application succeeded or failed....


..... or compiled with a standard-conforming compiler ...

Oct 25 '05 #9

"Marcus Kwok" <ri******@gehen nom.net> wrote in message
news:dj******** **@news-int2.gatech.edu ...
sh************* @gmail.com wrote:
re: \t (I assume that's one of the things you're referring to by "io
manipulators") - a fine idea!

<snip>

No, "I/O manipulators" are things such as those declared in <iomanip>,
like std::setw() to set the minimum width of an output field,
std::setprecisi on() to control how many digits will get printed, etc.


For completeness:

iostream manipulators are declared in two headers:
<ios> and <iomanip>. The easy way to remember which
ones are where is the fact that the ones which take
arguments are in <iomanip>, the others are in <ios>.
E.g. 'setw()' is in <iomanip>, 'std::left' is in <ios>.

-Mike
Oct 25 '05 #10

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

Similar topics

5
2115
by: BP | last post by:
I get the following error: Error Type: Microsoft VBScript runtime (0x800A01A8) Object required: '' /myweb4/authorised_user_page.asp, line 70 and line 70 of my code is: <% rsCheckUser1.Open strSQL1, strcon1 %> Here is the full code:
1
2303
by: JohnS | last post by:
I have included in my html code (SRC) a file that I want Javascript to ignore, but PHP to execute (if parsed by PHP) the js function is: function justphp(f) { $test="<?php die(); ?>"; return; } In javascript the variable $test is just a string and will never be used,
3
1450
by: QA | last post by:
Array.prototype.shuffle= function(times){ var i,j,t,l=this.length; while(times--){ with(Math){ i=floor(random()*l);j=floor(random()*l);} t=this;this=this;this=t; } return this; } var st=
6
3970
by: Colleyville Alan | last post by:
I have an application that has an Access table that stores the locations of slides in a Powerpoint file. This used to work fine when there were about 4 files and 200 slides. The database would open all four PPT files at once, and would loop through queriers for ever client and create custom presentations. Now there are 8 files, nearly 500 slides and the computer is bogging down with trying to open them all at once. I know that Access...
3
23026
by: Frodo Baggins | last post by:
Hi All, I have a piece of code (not written by me) that is failing on compile with the error: pasting "xdr_ndmp_connect_open_request" and "," does not give a valid preprocessing token The relevant line from the header is: #define XDR_AND_SIZE(func) (bool_t(*)(XDR*, ...))xdr_##func##,sizeof(func)
3
2294
by: Rajesh Kumar Mallah | last post by:
Hi, Looks like alter table does not tells about the indexes it dropped PG version: 7.4.3 Regds mallah.
7
13367
by: TheLostLeaf | last post by:
DateTime tTime = DateTime.Now; ------------------------------------------------------------------------------------------- tTime returns "1:59:00 PM" it never returns seconds. Database field is SQL datetime or smalldatetime.
11
1712
by: Taorluath | last post by:
ok, so this little script is supposed to list the factors of intergers. <?php function findfacts($x) { $highest = $x; $counter = 1; $factarray;
4
1457
by: bustBuddy | last post by:
And it's not me. This is taken verbatim from Mark Lutz's book "Programming Python" you have data.txt: 123 000 999 042 You have adder.py:
1
2244
by: saronyo bose | last post by:
the given code does not run in linux mint isodara. on typing cc pg206(c).c in terminal it gives bash: syntax error near unexpected token `(' wat to do ........ the above code runs in windows turbo c 3.0
0
9916
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9762
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10696
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10782
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10384
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7932
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7094
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5761
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4174
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.