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

Passing Pointers

Help!!!

I need someone to tell me why I am getting the err msg

error C2440: '=' : cannot convert from 'char *' to 'char'

//====================
#include <iostream>
using namespace std;

char *Input();
void Output(char *);

//====================
void main()
{

char *iptr = new char [100];
*iptr = Input();
Output(iptr);

delete [] iptr;

}

//====================

char *Input(void)
{

char *name = new char [100];
cout << "Enter name: ";
cin >> *name;
return name;

}

//====================

void Output(char *input)
{
cout << *input;
cout << endl;
}
Jul 22 '05 #1
17 2238
BlindHorse wrote:

Help!!!

I need someone to tell me why I am getting the err msg

error C2440: '=' : cannot convert from 'char *' to 'char'

//====================
#include <iostream>
using namespace std;

char *Input();
void Output(char *);

//====================
void main()
{

char *iptr = new char [100];
*iptr = Input();
Output(iptr);

delete [] iptr;

}

//====================

char *Input(void)
{

char *name = new char [100];
cout << "Enter name: ";
cin >> *name;
return name;

}

//====================

void Output(char *input)
{
cout << *input;
cout << endl;
}

Change:
*iptr = Input();

to:
iptr = Input();

and you should be good to go. You don't need to dereference the destination
iptr to char.

Also, you don't need to allocate:

char *iptr = new char [100];

Replace with

char *iptr = NULL;

otherwise you will leak memory when you do the (pointer) assignment to iptr.
Jul 22 '05 #2
On 3 Mar 2004 18:31:24 -0800, bl******@yahoo.com (BlindHorse) wrote:
Help!!!

I need someone to tell me why I am getting the err msg

error C2440: '=' : cannot convert from 'char *' to 'char'
Before we get started:
char c; // one character
char *cp = "abc"; // a pointer to characters (the 'a' initially)
c = *cp; // copy the 'a' into c
c = cp; // type mismatch: can't copy a pointer into a char
*cp = "abc"; // type mismatch: can't copy a pointer into a char
*cp = 'a'; // ok.

Now, then:

//====================
#include <iostream>
using namespace std;

char *Input();
void Output(char *);

//====================
void main()
int main()
{

char *iptr = new char [100];
You won't be needing that dynamic memory above, so just do:
char *iptr;
*iptr = Input();
Above, you're trying to force a pointer (returned by Input()) into a
character (the type of the expression *iptr). What you want is:

iptr = Input();

And now you know why you didn't want to allocate that 100 char array above:
you'd be leaking it now if you did.
Output(iptr);

delete [] iptr;
those are ok. Note the memory you're deleting was allocated within Input().
That's fine; just be aware of it.

And add, as good style:

return 0;
}

//====================

char *Input(void)
{

char *name = new char [100];
OK, so name now points to a dynamically allocated array of chars.
cout << "Enter name: ";
cin >> *name;
Lose the '*', because the way you wrote it, you're saying to read a single
character from cin and stuff it into the first location of that 100 char
array. without the *:
cin >> name;
you'll read text from cin until you encounter the first white space, and
store it in the memory pointed to by name....even if the input is more than
100 chars (which would have undefined behavior. It could then proceed to
erase your hard disk and be within its rights.) If you want to read white
space as part of that input string, as well as make sure you don't overrun
the buffer, look into various forms of getline, e.g.:

cin.getline(name, 100);
return name;
OK, now it is up to the caller to delete that memory.
}

//====================

void Output(char *input)
{
cout << *input;
Again, lose the *, or you'll be displaying a single character.
cout << endl;
}


HTH,
-leor
Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3
* bl******@yahoo.com (BlindHorse) schriebt:

I need someone to tell me why I am getting the err msg

error C2440: '=' : cannot convert from 'char *' to 'char'

Uhum. There are more serious problems than that in the code.


//====================
#include <iostream>
using namespace std;

char *Input();
Here is one big problem: who is responsible for deallocating the
memory returned by function 'Input'?

That problem leads to directly to a memory leak in your program,
discussed below.

Instead of 'char*', I suggest you make the return type 'std::string'.
void Output(char *);
Here there's a problem in usage. It's only by the good grace of
old C compatibility that this 'Output' function can be used with
a literal string as an argument, because the argument type says
that 'Output' is free to change the string. Instead use e.g.
'char const []' as argument type.

//====================
void main()
This is not allowed by the standard.

Function 'main' must have return type 'int'.

Nothing else.
{

char *iptr = new char [100];
*iptr = Input();
The basic misunderstanding seems to be that you're thinking in
terms of assigning an array, complete, whereas what the code
attempts to do is to assign a pointer.

'*iptr' does not refer to the 100 character array you've just
allocated, as you might think.

Instead, since 'iptr' is a pointer to 'char', '*iptr' is a single
'char'.

Namely, the first character in that array.
Figure.

Draw one box representing the 'iptr' variable.
Draw one hundred small boxes representing the array.
Draw an arrow from within 'iptr' to the first array element.

The compiler only uses what the arrow points directly at.

It doesn't know or care that there's 99 elements further on.
So you're trying to assign the result of function 'Input', which is
a pointer to char, to a single char.

The compiler won't have any of that unless you force it to.

Output(iptr);

delete [] iptr;

}
You can make this _compile_ by removing the dereferencing:
char *iptr = new char [100];
iptr = Input();
Output(iptr);

delete [] iptr;
But then the assignment replaces the address of the one hundred bytes
you allocated.

Then you don't have that address available any longer for deallocation.

And the 'delete' will just deallocate the array allocated by 'Input'.

So here you have the memory leak mentioned earlier on.
//====================

char *Input(void)
Style: 'void' for argument list is a C'ism best a-voided in C++.

{

char *name = new char [100];
cout << "Enter name: ";
cin >> *name;
return name;

}

//====================

void Output(char *input)
{
cout << *input;
cout << endl;
}

Many of the problems will go away simply by using 'std::string'
instead of 'char*'.

The function signatures would then be:
std::string Input();
void Output( std::string const& );
And you then need to #include <string> at the top of the program.

Hth.

Jul 22 '05 #4
"BlindHorse" <bl******@yahoo.com> wrote in message
news:3d*************************@posting.google.co m...
Help!!!

I need someone to tell me why I am getting the err msg

error C2440: '=' : cannot convert from 'char *' to 'char'

//====================
#include <iostream>
using namespace std;

char *Input();
void Output(char *);
void Output(const char *);
//====================
void main()
int main()
{

char *iptr = new char [100];
*iptr = Input();
iptr = Input();
Output(iptr);

delete [] iptr;

return 0; /* not mandatory, but I consider it 'good style' */
}

//====================

char *Input(void)
{

char *name = new char [100];
Omit this line. You've already allocated the memory in 'main()'
This function receives a pointer to that memory as its argument.
cout << "Enter name: ";
cin >> *name;
cin >> name;

but this is dangerous, as it does not protect against the user
entering more characters than you've allocated memory for.

Consider dropping the manual memory management and use
a std::string object instead, which handles all that for you.
return name;

}

//====================

void Output(char *input)
{
cout << *input;
cout << input << '\n';
cout << endl;
}


Contrast:

#include <iostream>
using std::cin;
using std::cout;

#include <string>
using std::string;
using std::getline;
/* now we don't need a return type, since we're passing
an argument by reference, the function can modify
the passed object 'in place' */
void Input(string& s)
{
cout << "Enter name: ";
getline(cin, s); /* the string will automatically
size itself as necessary */
}
/* Notice that here the parameter is declared 'const'
since this function does not modify it. This
is a 'safety net' the language gives you for free.
An attempt to modify 's' will produce a compile error */
void Output(const std::string& s)
{
cout << s << '\n';
}

int main()
{
string my_str; /* an empty string */
Input(my_str);
Output(my_str);
return 0;
} /* memory for 'my_str' is automatically released when the
function ends */
/* Note how the way I've ordered the functions obviates
the need for prototypes */
No need to muck around with pointers.

C++ has many features which make life easier and 'safer' for the
programmer. Use 'em. :-)

-Mike

Jul 22 '05 #5

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:kb*******************@newsread1.news.pas.eart hlink.net...
"BlindHorse" <bl******@yahoo.com> wrote in message
news:3d*************************@posting.google.co m... No need to muck around with pointers.

C++ has many features which make life easier and 'safer' for the
programmer. Use 'em. :-)

-Mike


I have similar problem and I cannot get over it because it is exercise with
preconditions (why c++ books has pure c exercises?!)

I guess Horse the Blind is also doing some exercise..;)
Jul 22 '05 #6

"Paavo P" <et************************@sserveri.fi> wrote in message
news:Jv*******************@news1.nokia.com...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:kb*******************@newsread1.news.pas.eart hlink.net...
"BlindHorse" <bl******@yahoo.com> wrote in message
news:3d*************************@posting.google.co m...
No need to muck around with pointers.

C++ has many features which make life easier and 'safer' for the
programmer. Use 'em. :-)

-Mike


I have similar problem


What problem exactly?
and I cannot get over it because it is exercise with
preconditions
What's wrong with preconditions? They create a 'formal'
context which helps keep your code correct.
(why c++ books has pure c exercises?!)


There are far more bad C++ books available than good ones.
Perhaps you need better ones. Take a look at the reviews
at www.accu.org

-Mike
Jul 22 '05 #7
Here's my prob:

function:

char* cat(char* a,char* b){

int size=(strlen(a)+strlen(b)+1);

char* ab=new char[size]; //reserve space with new-operator is in
preconditions
char* x=NULL;

while(*x++=*a++);
cout << "catC 1" << ab <<'\n';
*x--;
while(*x++=*b++);
cout << "catC 2" << ab <<'\n';

*ab=&x[0];
return ab; //return the result is in preconditions

} // end function cat

and the caller:
int main( int argc, char *argv[] )
{

char text1[]="Hallo";
char text2[]="Spaceboy";
char* madcat=NULL;
madcat=cat(text1,text2);

cout << "String " << text1 << " and string " << text2 << '\n';
cout << "" are concatenated as " << madcat <<'\n';

return( EXIT_SUCCESS );

} // end main

I followed the instructions but I cannot get it
Jul 22 '05 #8

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:FA*****************@newsread1.news.pas.earthl ink.net...

I have similar problem

See msg belower inthe thread.

What problem exactly?
and I cannot get over it because it is exercise with
preconditions


What's wrong with preconditions? They create a 'formal'
context which helps keep your code correct.
(why c++ books has pure c exercises?!)


There are far more bad C++ books available than good ones.
Perhaps you need better ones. Take a look at the reviews
at www.accu.org

-Mike


It was preconditions set by teacher (in this case it was the writer). This
exercise was 6.6.13 of Stroustrup's second edition.
Jul 22 '05 #9
"Paavo P" <et************************@sserveri.fi> wrote in message
news:iB*******************@news1.nokia.com...
Here's my prob:
Where are the headers?

function:

char* cat(char* a,char* b){

int size=(strlen(a)+strlen(b)+1);
Use type 'size_t' for sizes. It's declared by <cstdlib>
or <stdlib.h>


char* ab=new char[size]; //reserve space with new-operator is in preconditions
That's not a 'precondition', but a specification.
char* x=NULL;

while(*x++=*a++);
This produces undefined behavior, because you're dereferencing
a NULL pointer ('x').
cout << "catC 1" << ab <<'\n';
More undefined behavior. 'cout's 'operator<<()' for type char*
will dereference that pointer. But that pointer points to
memory which has never been initialized or given a value.
*x--;
More undefined behavior. Also, leaving that aside, you
have provided no protection against 'x' running off the
beginning of the array with that decrement.
while(*x++=*b++);
Yet MORE undefined behavior. 'x' still does not point
to memory with a valid value.
cout << "catC 2" << ab <<'\n';
More undefined behavior. 'ab' still doesn't point to anything
with a valid value.

*ab=&x[0];
And again. Twice in one statement.
return ab; //return the result is in preconditions
That's a specification, not a precondition.

} // end function cat

and the caller:
int main( int argc, char *argv[] )
{

char text1[]="Hallo";
char text2[]="Spaceboy";
char* madcat=NULL;
madcat=cat(text1,text2);

cout << "String " << text1 << " and string " << text2 << '\n';
cout << "" are concatenated as " << madcat <<'\n';

return( EXIT_SUCCESS );

} // end main

I followed the instructions but I cannot get it


Well, you either did *not* follow instructions, or
your instructions were very poor. Perhaps you could
post the exact instructions you were given.

What you've posted here will not even compile, let alone work.

Also, if this is exercise is supposed to be teaching you C++,
it's a very poor way to do so. You shouldn't be
being taught to do all this low level memory management
(at least not until much later), but to use the standard
library for string handling. It looks like your instructor
and/or book cannot seem to distinguish C from C++.

-Mike
Jul 22 '05 #10

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:mT*******************@newsread1.news.pas.eart hlink.net...
"Paavo P" <et************************@sserveri.fi> wrote in message
news:iB*******************@news1.nokia.com... Well, you either did *not* follow instructions, or
your instructions were very poor. Perhaps you could
post the exact instructions you were given.

What you've posted here will not even compile, let alone work.
I followed instructions, but it wouldn't work so I just tried everything
till got totally lost:/
Also, if this is exercise is supposed to be teaching you C++,
it's a very poor way to do so. You shouldn't be
being taught to do all this low level memory management
(at least not until much later), but to use the standard
library for string handling. It looks like your instructor
and/or book cannot seem to distinguish C from C++.

-Mike


I would do it this way:

first two char arrays (a and b) and then

string s;
s.append(a);
s.append(b);
cout << s <<'\n';

simple and elegant with C++ and I like it.
Jul 22 '05 #11

"Paavo P" <et************************@sserveri.fi> wrote in message
news:eo*******************@news1.nokia.com...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:mT*******************@newsread1.news.pas.eart hlink.net...
"Paavo P" <et************************@sserveri.fi> wrote in message
news:iB*******************@news1.nokia.com...

Well, you either did *not* follow instructions, or
your instructions were very poor. Perhaps you could
post the exact instructions you were given.

What you've posted here will not even compile, let alone work.


I followed instructions, but it wouldn't work so I just tried everything
till got totally lost:/


Will you show us these instructions?
Also, if this is exercise is supposed to be teaching you C++,
it's a very poor way to do so. You shouldn't be
being taught to do all this low level memory management
(at least not until much later), but to use the standard
library for string handling. It looks like your instructor
and/or book cannot seem to distinguish C from C++.

-Mike


I would do it this way:

first two char arrays (a and b) and then

string s;
s.append(a);
s.append(b);
cout << s <<'\n';

simple and elegant with C++ and I like it.


string s;
s += "Hello";
s += " world";

Hardly worth writing a function for that. :-)

I always wonder, why are so many C++ books and
instructors so enamored with C-style strings,
despite the existence of the far superior
std::string type?

-Mike
Jul 22 '05 #12

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:kx******************@newsread1.news.pas.earth link.net...
|
| "Paavo P" <et************************@sserveri.fi> wrote in message
| news:eo*******************@news1.nokia.com...

[snip]

| > I would do it this way:
| >
| > first two char arrays (a and b) and then
| >
| > string s;
| > s.append(a);
| > s.append(b);
| > cout << s <<'\n';
| >
| > simple and elegant with C++ and I like it.
|
| string s;
| s += "Hello";
| s += " world";
|
| Hardly worth writing a function for that. :-)

And if we wanted to be cute :-):
std::string s( std::string( a ) + b );

| I always wonder, why are so many C++ books and
| instructors so enamored with C-style strings,
| despite the existence of the far superior
| std::string type?

Agreed :-).

Cheers.
Chris Val
Jul 22 '05 #13
Paavo P wrote:
It was preconditions set by teacher (in this case it was the writer). This
exercise was 6.6.13 of Stroustrup's second edition.


Why is the instructor not using the 3rd edition. That one is up-to-date
with the current standard.
Brian Rodenborn
Jul 22 '05 #14

"Default User" <fi********@boeing.com.invalid> wrote in message
news:40***************@boeing.com.invalid...
Paavo P wrote:
It was preconditions set by teacher (in this case it was the writer). This exercise was 6.6.13 of Stroustrup's second edition.


Why is the instructor not using the 3rd edition. That one is up-to-date
with the current standard.
Brian Rodenborn


3rd was not available in Finnish.
Jul 22 '05 #15

"Paavo P" <et************************@sserveri.fi> wrote in message
news:iB*******************@news1.nokia.com...
Here's my prob:

function:

char* cat(char* a,char* b){

int size=(strlen(a)+strlen(b)+1);

char* ab=new char[size]; //reserve space with new-operator is in
preconditions
Good so far.
char* x=NULL;

while(*x++=*a++);


Well you are obviously trying to copy the memory pointed to by a to the
memory pointed to by ab using the pointer x. The problem is that you have
not made x point to the place you want to start copying from (i.e. a). Also
the 'cute' but terse syntax *x++ = *a++ is just too confusing. Try this

char* x = a;
while (*a != '\0') // while not at the end of string
{
*x = *a; // copy one char from a to x
++x; // move x on
++a; // move a on
}

john
Jul 22 '05 #16
"Paavo P" <et************************@sserveri.fi>

Why is the instructor not using the 3rd edition. That one is up-to-date
with the current standard.


3rd was not available in Finnish.


It has been for a while now: Finnish translation 2000; Teknolit; ISBN 951-846-026-4.

-- Bjarne Stroustrup; http://www.research.att.com/~bs
Jul 22 '05 #17
John Harrison wrote:

"Paavo P" <et************************@sserveri.fi> wrote in message
news:iB*******************@news1.nokia.com...
Here's my prob:

function:

char* cat(char* a,char* b){

int size=(strlen(a)+strlen(b)+1);

char* ab=new char[size]; //reserve space with new-operator is in
preconditions
Good so far.
char* x=NULL;

while(*x++=*a++);


Well you are obviously trying to copy the memory pointed to by a to the
memory pointed to by ab using the pointer x. The problem is that you have
not made x point to the place you want to start copying from (i.e. a).


As x represents the target for the copy operation, it should be initialised
to ab.
Also the 'cute' but terse syntax *x++ = *a++ is just too confusing. Try
this

char* x = a;
Change this to
char* x = ab;
while (*a != '\0') // while not at the end of string
{
*x = *a; // copy one char from a to x
++x; // move x on
++a; // move a on
}

john


Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://ma.rtij.nl/acllc-c++.FAQ.html
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
Jul 22 '05 #18

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
2
by: Morgan | last post by:
Thanks to all of you because I solved the problem related with my previous post. I simply made confusion with pointers to pointers and then succeeded passing the reference to the first element...
2
by: LeTubs | last post by:
Hi I'm think I'm having some problem with pointers..... namely passing them around...Or I think ? I've used gdb to find the following (note conn is of type MYSQL *conn). Program received...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
1
by: Lucy Ludmiller | last post by:
I have a class CMyObject, that I want to pass in an ActiveX event. I have not as yet found a way of passing pointers in events - any one knows how ? //PseudoCode IDL void...
8
by: Ivan Liu | last post by:
Hi, I'd like to ask if passing an object as an pointer into a function evokes the copy constructor. Ivan
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
7
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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
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...
0
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...

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.