Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Invalid conversion from 'char' tp 'char*'

Reply
Thread Tools

Invalid conversion from 'char' tp 'char*'

 
 
Jack Klein
Guest
Posts: n/a
 
      04-08-2004
On Wed, 7 Apr 2004 20:35:28 +0000 (UTC), Christopher Benson-Manica
<(E-Mail Removed)> wrote in comp.lang.c++:

> Tim Johansson <(E-Mail Removed)> spoke thus:
>
> > #include <iostream.h>

>
> It's called <iostream> - the .h headers are deprecated.
>
> > #include <string.h>

>
> > int main () {
> > srand(time(0));
> > int array_length;
> > int count;
> > int randm;
> > char temp[30];
> > cout << "How many items in array? ";
> > cin >> array_length;
> > char items [30][array_length + 1];

> ^^^^^^^^^^^^
> I assume this is legal in C++, but I'd love it if a real C++ person
> stated something authoratative. As an aside, if you're making the
> array array_length+1 items long, then array_length isn't really, is
> it?


No, it is not legal in C++. It is legal in C under the
not-widely-implemented C99 version of the standard, where arrays with
automatic storage class can be VLAs (Variable Length Arrays). It is
also a non-standard extension provided by GNU and perhaps other
compilers.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Old Wolf
Guest
Posts: n/a
 
      04-08-2004
Christopher Benson-Manica <(E-Mail Removed)> wrote:
> Tim Johansson <(E-Mail Removed)> spoke thus:
>
> > char items [30][array_length + 1];

> ^^^^^^^^^^^^
> I assume this is legal in C++, but I'd love it if a real C++ person
> stated something authoratative. As an aside, if you're making the
> array array_length+1 items long, then array_length isn't really, is
> it?


Well it isn't the array length anyway: he would have declared an array
of 30 items, each item of which has length (array_length+1), if the
syntax were legal.

> > for (count = 0; count < array_length; count++) {
> > randm = rand() % array_length;
> > strcpy (temp[30], items[30][count]);

>
> Here's the problem (well, besides that you're using C-style strings at
> all): strcpy is prototyped as
>
> char *strcpy( char *s, const char *t );
>
> What is temp[30]? It's a char, of course. And what is
> items[30][count]? It's a char, too. No wonder it doesn't compile,
> eh?


Not to mention that both are undefined behaviour (reading a char
from past the end of the array). God knows what he was trying to do there.

> Either read a C book or learn how to use std::strings and std::vectors
> - I recommend the latter.


Seeing code like this is what moved me from the "Learn C then C++" camp
to the "Learn C++ then C" camp. Petec's replacement program is
excellent.
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      04-08-2004
Kevin, someone should be paying you.

john


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      04-08-2004

"Tim Johansson" <(E-Mail Removed)> wrote in message
news:c51l09$2nosfd$(E-Mail Removed)-berlin.de...
> I'm new to C++, and tried to start making a script that will shuffle an
> array. Can someone please tell me what's wrong?
>


You've been give lots of good advice, but clearly the best advice is to find
better material for your learning.

1) Get a good book, one that doesn't say that <iostream.h> is the right
thing to do. Also one that mentions standard library templates such as
string and vector (these are what you should be using for this program).

2) Get a better compiler. The compiler you are using is allowing you to get
away with illegal code. What sort of way to learn C++ is that? Now I'm
guessing here but if you are using gcc or g++ then you should compile with
the -ansi and -pedantic options. This will make your compiler much closer to
the real C++ language.

john


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      04-08-2004
John Harrison wrote:

> Kevin, someone should be paying you.
>
> john
>
>


Well, I certainly think so. But not for what I'm doing here. I think
we all do this for basically the same reason: because learning is hard,
because we want to see the overall quality of programmers improve, and
because we want to give back to the group that is always here to help us
when we need it.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      04-08-2004
Kevin Goodsell <(E-Mail Removed)> spoke thus:

> Well, I certainly think so. But not for what I'm doing here. I think
> we all do this for basically the same reason: because learning is hard,
> because we want to see the overall quality of programmers improve, and
> because we want to give back to the group that is always here to help us
> when we need it.


You forgot some reasons: learning is fun, writing elegant code is fun,
and engaging in arcane pedantry is fun

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
invalid character in base-64 string and invalid postback or callba kevin ASP .Net 0 01-16-2008 09:39 PM
How do fix: "Wrong number of arguments" / "invalid propert assignment"/ "Invalid argument" Java Guy Javascript 1 10-15-2006 08:41 PM
How do fix: "Wrong number of arguments" / "invalid propert assignment"/ "Invalid argument" Java Guy Java 1 10-15-2006 08:31 PM
Invalid URI: There is an invalid sequence in the string. Error!! Patrick.O.Ige ASP .Net 1 07-02-2006 11:21 AM
invalid@invalid.org Hank Sniadoch Computer Information 1 08-20-2004 11:24 AM



Advertisments