C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Passing Pointers
Goto page 1, 2, 3 ... 16, 17, 18  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
BlindHorse
Guest





PostPosted: Thu Mar 04, 2004 2:31 am    Post subject: Passing Pointers Reply with quote



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;
}
Back to top
Julie
Guest





PostPosted: Thu Mar 04, 2004 2:37 am    Post subject: Re: Passing Pointers Reply with quote



BlindHorse wrote:
Quote:

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.

Back to top
Leor Zolman
Guest





PostPosted: Thu Mar 04, 2004 2:51 am    Post subject: Re: Passing Pointers Reply with quote



On 3 Mar 2004 18:31:24 -0800, [email]blndhrse (AT) yahoo (DOT) com[/email] (BlindHorse) wrote:

Quote:
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:

Quote:



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

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

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

int main()

Quote:
{

char *iptr = new char [100];

You won't be needing that dynamic memory above, so just do:
char *iptr;

Quote:
*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.

Quote:
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;
Quote:

}

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

char *Input(void)
{

char *name = new char [100];

OK, so name now points to a dynamically allocated array of chars.

Quote:
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);

Quote:
return name;

OK, now it is up to the caller to delete that memory.
Quote:

}

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

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

Again, lose the *, or you'll be displaying a single character.

Quote:
cout << endl;
}

HTH,
-leor


Leor Zolman
BD Software
[email]leor (AT) bdsoft (DOT) com[/email]
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

Back to top
Alf P. Steinbach
Guest





PostPosted: Thu Mar 04, 2004 2:53 am    Post subject: Re: Passing Pointers Reply with quote

* [email]blndhrse (AT) yahoo (DOT) com[/email] (BlindHorse) schriebt:
Quote:

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.


Quote:

//====================
#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'.



Quote:
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.



Quote:

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

This is not allowed by the standard.

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

Nothing else.



Quote:
{

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.


Quote:
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.



Quote:
//====================

char *Input(void)

Style: 'void' for argument list is a C'ism best a-voided in C++.


Quote:
{

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
Hth.


Back to top
Mike Wahler
Guest





PostPosted: Thu Mar 04, 2004 3:15 am    Post subject: Re: Passing Pointers Reply with quote

"BlindHorse" <blndhrse (AT) yahoo (DOT) com> wrote

Quote:
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 *);

Quote:
//====================
void main()

int main()

Quote:
{

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

iptr = Input();

Quote:
Output(iptr);

delete [] iptr;


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

Quote:
}

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

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.

Quote:
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.

Quote:
return name;

}

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

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

cout << input << 'n';

Quote:
cout << endl;
}

Contrast:

#include 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




Back to top
Paavo P
Guest





PostPosted: Thu Mar 04, 2004 9:18 am    Post subject: Re: Passing Pointers Reply with quote


"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net> wrote

Quote:
"BlindHorse" <blndhrse (AT) yahoo (DOT) com> wrote in message
news:3d8b07ef.0403031831.65a778e (AT) posting (DOT) google.com...

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..;)



Back to top
Mike Wahler
Guest





PostPosted: Thu Mar 04, 2004 9:23 am    Post subject: Re: Passing Pointers Reply with quote


"Paavo P" <etunimi.keskinimi.takanimi (AT) sserveri (DOT) fi> wrote

Quote:

"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net> wrote in message
news:kbx1c.20500$aT1.12413 (AT) newsread1 (DOT) news.pas.earthlink.net...
"BlindHorse" <blndhrse (AT) yahoo (DOT) com> wrote in message
news:3d8b07ef.0403031831.65a778e (AT) posting (DOT) google.com...

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?

Quote:
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.

Quote:
(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



Back to top
Paavo P
Guest





PostPosted: Thu Mar 04, 2004 9:24 am    Post subject: Re: Passing Pointers Reply with quote

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


Back to top
Paavo P
Guest





PostPosted: Thu Mar 04, 2004 9:41 am    Post subject: Re: Passing Pointers Reply with quote


"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net> wrote

Quote:


I have similar problem

See msg belower inthe thread.

Quote:

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.



Back to top
Mike Wahler
Guest





PostPosted: Thu Mar 04, 2004 9:43 am    Post subject: Re: Passing Pointers Reply with quote

"Paavo P" <etunimi.keskinimi.takanimi (AT) sserveri (DOT) fi> wrote

Quote:
Here's my prob:

Where are the headers?

Quote:

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>


Quote:

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

preconditions

That's not a 'precondition', but a specification.

Quote:
char* x=NULL;

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

This produces undefined behavior, because you're dereferencing
a NULL pointer ('x').

Quote:
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.

Quote:
*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.

Quote:
while(*x++=*b++);

Yet MORE undefined behavior. 'x' still does not point
to memory with a valid value.

Quote:
cout << "catC 2" << ab <<'n';

More undefined behavior. 'ab' still doesn't point to anything
with a valid value.

Quote:

*ab=&x[0];

And again. Twice in one statement.

Quote:
return ab; //return the result is in preconditions

That's a specification, not a precondition.

Quote:

} // 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



Back to top
Paavo P
Guest





PostPosted: Thu Mar 04, 2004 10:18 am    Post subject: Re: Passing Pointers Reply with quote


"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net> wrote

Quote:
"Paavo P" <etunimi.keskinimi.takanimi (AT) sserveri (DOT) fi> wrote in message
news:iBC1c.10682$k4.227513 (AT) news1 (DOT) 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:/

Quote:
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.



Back to top
Mike Wahler
Guest





PostPosted: Thu Mar 04, 2004 10:28 am    Post subject: Re: Passing Pointers Reply with quote


"Paavo P" <etunimi.keskinimi.takanimi (AT) sserveri (DOT) fi> wrote

Quote:

"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net> wrote in message
news:mTC1c.20811$aT1.11267 (AT) newsread1 (DOT) news.pas.earthlink.net...
"Paavo P" <etunimi.keskinimi.takanimi (AT) sserveri (DOT) fi> wrote in message
news:iBC1c.10682$k4.227513 (AT) news1 (DOT) 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?

Quote:
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



Back to top
Chris ( Val )
Guest





PostPosted: Thu Mar 04, 2004 11:09 am    Post subject: Re: Passing Pointers Reply with quote


"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net> wrote

Quote:

"Paavo P" <etunimi.keskinimi.takanimi (AT) sserveri (DOT) fi> wrote in message
news:eoD1c.10696$k4.227388 (AT) news1 (DOT) nokia.com...

[snip]

Quote:
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. Smile

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

Quote:
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 Smile.

Cheers.
Chris Val



Back to top
Default User
Guest





PostPosted: Thu Mar 04, 2004 7:40 pm    Post subject: Re: Passing Pointers Reply with quote

Paavo P wrote:

Quote:
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

Back to top
Paavo P
Guest





PostPosted: Fri Mar 05, 2004 8:18 am    Post subject: Re: Passing Pointers Reply with quote


"Default User" <first.last (AT) boeing (DOT) com.invalid> wrote

Quote:
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.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2, 3 ... 16, 17, 18  Next
Page 1 of 18

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.