473,382 Members | 1,387 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,382 software developers and data experts.

Extremely Simple Question

what's wrong with this, and .. can you not declare "char" variables?

#include <iostream>

int main()
{
int a = "* * * * * * * *\n";
std::cout << a;
return EXIT_SUCCESS;
}

thx
Jul 19 '05 #1
10 1820
Mike Lundell wrote:
what's wrong with this, and .. can you not declare "char" variables?

blah blah blah

oops, sorry for duel post.. my time is off on my box so i didn't see the
post as most recent.
Jul 19 '05 #2
Mike Lundell wrote:
what's wrong with this, and .. can you not declare "char" variables?

#include <iostream>

int main()
{
int a = "* * * * * * * *\n";
std::cout << a;
return EXIT_SUCCESS;
}

thx

You cannot convert a string to an int.

"* * * * * * * *\n" is from type char*,
which means that you can represent this
string as a pointer on the first character.
A function or method, that would handle
with this string, would start from this
first pointer and would go along this
string until it finds the end of the
string, which is marked with 0 (don't
mix it up with "0", which would be the
character "0").

What you can do, is somethind like

int a = 'k';

which convert the 'k' into it's ASCII
number. That's why you can also do
things like 'if ('a' < 'b') std::cout <<
"hello world"'.
This works only with single characters
and not strings.

solution: replace

int a = "* * * * * * * *\n";

with

char* a = "* * * * * * * *\n";

bye Boris

Jul 19 '05 #3


Allan Bruce wrote:

char* a = "* * * * * * * *\n";

bye Boris

This is wrong too - you either need


why?
char a[] = "* * * * * * * *\n";

or

char *a = new char[16] = "* * * * * * * *\n";
That, my friend, is seriously wrong. You can't assign
arrays in C++, not even character arrays.

char *a = strcpy( new char[17], "* * * * * * * *\n" );

would work, but it's not a good idea most of the time.


The only thing that could be made better in

char* a = "* * * * * * * *\n";

is to replace the data type of the pointer to

const char* a = "* * * * * * * *\n";

but other then that, Boris is completely correct.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4
Allan Bruce wrote in news:bg**********@news.freedom2surf.net:

or

char *a = new char[16] = "* * * * * * * *\n";


#include <iostream>
#include <ostream>

int main()
{
char *a = "* * * * * * * *\n";
char *b = new char[16] = "* * * * * * * *\n";
char *c = new char[16];

std::cout << "a: " << ((void *)a) << std::endl;
std::cout << "b: " << ((void *)b) << std::endl;
std::cout << "c: " << ((void *)c) << std::endl;
}

The output I get (MSVC 7.1) is:

a: 0041E01C
b: 0041E030
c: 002F0F88

I was supprised that my compiler even let me write this nonsense.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #5
Rob Williscroft wrote:
char[16]


is that like a new instance of the "char" class defined as char *c?

if so, i'm guessing the 16 is for how many characters are defined for that
var. (much like in mySQL). Anyway, this is all guessing on that part.
but that does makes sense to me if that's what it is. I haven't started my
college course yet, so that's why I don't know this stuff, i'm sure. But,
one more question.. what does the "*" do to the "c"? I'm guessing, it's
some sort of alias pointer.. but even if that's what it is. It makes no
sense to me, why you would need an "alias" if the letter c was the variable
name?!.. I'm really confused on that part. And I know this isn't good, my
first language I learned was vb (don't tell anyone that lol, i'm rather
embarased). And, so, now it's kind of hard to accept the way new languages
do things, especially when i've never seen it before. could someone
clarify what exactly the "*" does please? thank you in advanced.
Jul 19 '05 #6
Mike Lundell wrote in news:vi************@corp.supernews.com:
Rob Williscroft wrote:
char[16]
is that like a new instance of the "char" class defined as char *c?


char my_array[16]; // an array of 16 char's
char *p = my_array;

p 'points to' the first element of 'my_array'.

in C++ you can reference the array's elements like this:

char c = my_array[ n ]; // where 0 <= n and n < 16.

C++ allows you to do the same with pointers (i.e 'p' above):

char c2 = p[ n ];

If you want to have an array that lives longer than the function
you create it in then you can write:

char *dynamic_p = new char[16];

dynamic_p now 'points to' an (unnamed if you like) array that the
compiler has created. To get rid of the array, once you're finnished
with it, write:

delete [] dynamic_p;

if so, i'm guessing the 16 is for how many characters are defined for
that var. (much like in mySQL). Anyway, this is all guessing on that
part. but that does makes sense to me if that's what it is. I haven't
started my college course yet, so that's why I don't know this stuff,
If you want a head start before you start your course I'd suggest
getting a book, maybe one you're going to have to get for course
anyway, It'll be a lot faster than trying to learn via usenet.
i'm sure. But, one more question.. what does the "*" do to the "c"?
It changes the type of the variable being declared.

char c; // in words: c is of type char.
char *p; // in words: p is of type pointer to char.
I'm guessing, it's some sort of alias pointer.. but even if that's
what it is. It makes no sense to me, why you would need an "alias" if
the letter c was the variable name?!.. I'm really confused on that
part. And I know this isn't good, my first language I learned was vb
(don't tell anyone that lol, i'm rather embarased). And, so, now it's
kind of hard to accept the way new languages do things, especially
when i've never seen it before. could someone clarify what exactly
the "*" does please? thank you in advanced.


This post hasn't covered 1% of "what exactly the "*" does", so get
a good book and go to that course.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #7
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<bg**********@news.freedom2surf.net>...

char* a = "* * * * * * * *\n";

bye Boris


This is wrong too - you either need

char a[] = "* * * * * * * *\n";

or

char *a = new char[16] = "* * * * * * * *\n";

Allan


or const char *a = "* * * * * * * *\n";

GJD
Jul 19 '05 #8
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
in message news:<bg**********@news.freedom2surf.net>...
char* a = "* * * * * * * *\n";
This is wrong too - you either need


It is indeed wrong but for other reasons than those you suggest: the type
of a string literal is 'char const[n]' (where 'n' is the number of characters
occupied by the string). You thus cannot assign it to a 'char*'. Well,
actually I think you can but this is a deprecated feature. You should write

char const* a = "* * * * * * * *\n";

instead.
char a[] = "* * * * * * * *\n";
This is something quite different: the above approach just obtains a pointer
to string literal while what you are doing here is to create an array of
characters which is initialized by copying the string literal.
or
char *a = new char[16] = "* * * * * * * *\n";


.... and this is utter nonsense, supposed to be rejected by the compiler: the
result of 'new char[16]' is not an lvalue and thus you cannot assign to it.
Even if it were an lvalue it would have the wrong semantic: it would assign
the pointer to the string literal discarding the pointer to the just
allocated memory thereby creating a resource leak.

BTW, the string literal you used above has 17 characters, not 16! You forgot
the terminating null character.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 19 '05 #9
Rob Williscroft wrote:
char c; // in words: c is of type char.
char *p; // in words: p is of type pointer to char.


Thanks tons for the response Rob. It makes a lot more sense now. :)
Jul 19 '05 #10
> Allan Bruce wrote in news:bg**********@news.freedom2surf.net:

or

char *a = new char[16] = "* * * * * * * *\n";


#include <iostream>
#include <ostream>

int main()
{
char *a = "* * * * * * * *\n";
char *b = new char[16] = "* * * * * * * *\n";
char *c = new char[16];

std::cout << "a: " << ((void *)a) << std::endl;
std::cout << "b: " << ((void *)b) << std::endl;
std::cout << "c: " << ((void *)c) << std::endl;
}

The output I get (MSVC 7.1) is:

a: 0041E01C
b: 0041E030
c: 002F0F88

I was supprised that my compiler even let me write this nonsense.

Rob.


I was surprised it compiles with g++ as well, but I do get a compiler
warning.

My guess is that (with MSVC 7.1) if you print out b ( and not *b ) it
will not be what you expect, ie not "* * * * * * * * * *\n"

-Brian
Jul 19 '05 #11

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

Similar topics

0
by: smartin | last post by:
python IIS cgi loading extremely slow I recently uninstalled python 1.5.2 and installed python 2.3 on a Windows 2000 server running IIS 5.0. The issue is that when submitting a cgi request to a...
7
by: mikester | last post by:
First off I'll say - I am a bad perl programmer. I want to be better and with your help I'll get there and then be able to contribute more here. That being said, I have a simple problem...
3
by: rusttree | last post by:
Many moons ago, I took a class in embedded control at school. The course focused on a micro-controller mounted on a small electric car that was programmed using simple C code. The...
83
by: D. Dante Lorenso | last post by:
Trying to use the 'search' in the docs section of PostgreSQL.org is extremely SLOW. Considering this is a website for a database and databases are supposed to be good for indexing content, I'd...
2
by: Segfahlt | last post by:
I have a fairly simple C# program that just needs to open up a fixed width file, convert each record to tab delimited and append a field to the end of it. The input files are between 300M and...
8
by: Jack | last post by:
When I try TooFPy with the SOAP and XML-RPC sample client code provided in TooFPy tutorials, a log entry shows up quickly on web server log window, but it takes a long time (5 seconds or longer)...
5
by: coldpizza | last post by:
I am trying to fill a sqlite3 database with records, and to this end I have written a class that creates the db, the table and adds rows to the table. The problem is that the updating process is...
2
by: Todd.Branchflower | last post by:
Hey everyone, I am new to c++ and the group and would appreciate your help. I was doing a problem on projecteuler.net in which you had to find the largest factor of an extremely large number. ...
7
by: Bram2 | last post by:
Sorry if this sounds like a dumbass newbie question. I'm getting crazy or something, but somehow I can't get this simple thing to work: 1. In general, how do I center text and/or images...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.