From: voodoo31 on
The last line of my code below c = "Paul"; causes this error, Lvalue
required in function main().

Wondering why I cant assign a string literal in a case like this. It
seems similar to what I am do for both a and b.

Can anyone tell me more about why this does not work in C++?

#include <iostream>
using namespace std;

int main() {
char a[10] = "John";
cout << a << "\n";
char b[10];
cout << "Enter a name ";
cin >> b;
cout << b << "\n";
char c[10];
c = "Paul";
}

From: Jerry Coffin on
In article <4fd79c38-0038-4f4e-9abb-
26f9e508808e(a)e6g2000prf.googlegroups.com>, jflew(a)bigpond.com says...
> The last line of my code below c = "Paul"; causes this error, Lvalue
> required in function main().
>
> Wondering why I cant assign a string literal in a case like this. It
> seems similar to what I am do for both a and b.

You can initialize an array, but you cannot assign to one.

> Can anyone tell me more about why this does not work in C++?

Because C++ doesn't allow it. The obvious advice would be to use
instances of std::string instead:

#include <iostream>
#include <string>

using namespace std; // if you must...

int main() {
std::string a("John");
cout << a << "\n";
std::string b;
cout << "Enter a name: ";
cin >> b; // you probably want std::getline(cin, b)
cout << b << "\n";
std::string c;
c = "Paul";
return 0;
}

--
Later,
Jerry.

The universe is a figment of its own imagination.
From: Francis Glassborow on
voodoo31 wrote:
> The last line of my code below c = "Paul"; causes this error, Lvalue
> required in function main().
>
> Wondering why I cant assign a string literal in a case like this. It
> seems similar to what I am do for both a and b.
>
> Can anyone tell me more about why this does not work in C++?
>
> #include <iostream>
> using namespace std;
>
> int main() {
> char a[10] = "John";
> cout << a << "\n";
> char b[10];
> cout << "Enter a name ";
> cin >> b;
> cout << b << "\n";
> char c[10];
> c = "Paul";
> }
>
But is, in fact completely different. The a and b cases are
intialisations which both C and C++ treat as completely different actions.

However in C++ we normally avoid using arrays of char, not least because
lines such as cin >> b; are potential for complete disaster if the user
inputs more than 9 consecutive non whitespace characters.

If you get into the habit of using std::string to store and manipulate
strings things will behave the way you expect.
From: Jim Langston on
voodoo31 wrote:
> The last line of my code below c = "Paul"; causes this error, Lvalue
> required in function main().
>
> Wondering why I cant assign a string literal in a case like this. It
> seems similar to what I am do for both a and b.
>
> Can anyone tell me more about why this does not work in C++?
>
> #include <iostream>
> using namespace std;
>
> int main() {
> char a[10] = "John";
> cout << a << "\n";
> char b[10];
> cout << "Enter a name ";
> cin >> b;
> cout << b << "\n";
> char c[10];
> c = "Paul";
> }

c referes to an array of 10 characters. "Paul" refers to a character
pointer. Apples and oranges. Now, if c was a character pointer instead it
would actually compile and execute, but not do what you expected.

You do not want to load c with the pointer address of the 'P' in "Paul", but
the contents of the c-style string at the pointer.

For c-style strings this is normally done with one of the str commands.
strcpy, strncpy. strncpy is considered a little safer.

char c[10];
strncpy( c, "Paul", 10 );

strcpy and strncpy look at where the char pointer is pointing to and copy
each character one by one until it reachest the null terminator (or for
strncpy, the max length passed in, whichever comes first).

If you were using C++ and std::string then you could actually do it your
way.

std::string c;
c = "Paul";
is legal in C++ (but not C since C doesn't have the STL library).


--
Jim Langston
tazmaster(a)rocketmail.com


From: Bart van Ingen Schenau on
Jim Langston wrote:

> For c-style strings this is normally done with one of the str
> commands.
> strcpy, strncpy. strncpy is considered a little safer.

but strncpy also has its own pitfalls.
When provided with the correct arguments, strncpy can not cause a buffer
overrun, but it still can result in a failure to provide the
required '\0' character to terminate the destination string.

>
> char c[10];
> strncpy( c, "Paul", 10 );
>
> strcpy and strncpy look at where the char pointer is pointing to and
> copy each character one by one until it reachest the null terminator
> (or for strncpy, the max length passed in, whichever comes first).

This description of the behaviour of strncpy is not fully accurate.
strncpy copies at most N characters from the source to the destination.
If the source string is less than N characters long, the remainder of
the destination is filled with '\0'.
If the source is longer than N, the destination buffer will *not*
contain a '\0' character and contains therefor not a valid string.

This last part is something people tend to forget when describing
strncpy.

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
 | 
Pages: 1
Prev: String comparison
Next: Book for a beginner