I need some basic C++ help

P

Psykarrd

I am trying to declare a string variable as an array of char's. the
code looks like this.

char name[80];

then when i try to use the variable it dosn't work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";


thanks for any help you can give me.
 
D

Dave

Psykarrd said:
I am trying to declare a string variable as an array of char's. the
code looks like this.

char name[80];

then when i try to use the variable it dosn't work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";


thanks for any help you can give me.

strcpy(name, "modem");

An expression such as "modem" is viewed by the compiler as a const char *
(which points to an address in memory where the string literal "modem" is
stored).

So, the expression you gave was trying to assign an address to array. As
such an operation does not make sense to do, your compiler yelped!
 
A

Andrey Tarasevich

Psykarrd said:
I am trying to declare a string variable as an array of char's. the
code looks like this.

char name[80];

then when i try to use the variable it dosn't work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";
...

Both 'name' and "modems" are _arrays_. Arrays in C++ are not assignable.
In order to copy one array to another in general case you have to copy
them "manually" - element by element. The good news is that in most
cases there's a library function that can do it for you.

In this particular case you can use 'strcpy':

strcpy(name, modem);
 
D

Dave

Dave said:
Psykarrd said:
I am trying to declare a string variable as an array of char's. the
code looks like this.

char name[80];

then when i try to use the variable it dosn't work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";


thanks for any help you can give me.

strcpy(name, "modem");

An expression such as "modem" is viewed by the compiler as a const char *
(which points to an address in memory where the string literal "modem" is
stored).

So, the expression you gave was trying to assign an address to array. As
such an operation does not make sense to do, your compiler yelped!

Oops, sorry, I should have said "array of const char" instead of "const char
*". So, it's really an array, and *then* that array undergoes a conversion
to a pointer when the compiler encounters "modem". Finally then, as I said
before, a pointer cannot be assigned to an array...
 
J

John Carson

Dave said:
Oops, sorry, I should have said "array of const char" instead of
"const char
*". So, it's really an array, and *then* that array undergoes a
conversion to a pointer when the compiler encounters "modem".
Finally then, as I said before, a pointer cannot be assigned to an
array...

It is not the chars that are const --- if they were you couldn't change the
char values. Rather, it is the location in memory of the chars that cannot
be changed. In pointer terms, it is like a const pointer to char, i.e.,
char * const
 
M

Mike Wahler

Psykarrd said:
I am trying to declare a string variable as an array of char's. the
code looks like this.

char name[80];

then when i try to use the variable it dosn't work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";

Arrays are not assignable.

strcpy(name, "modem"); /* 'strcpy()' is declared by <cstring>
or <string.h>

Better yet, use the std::string type, which *is* assignable,
and also has the advantage of not being restricted to a fixed
size like an array:

std::string name; /* 'std::string' declared by <string> */
name = "modem";

Or if "modem" is to be the inital value:

std::string name("modem");

-Mike
 
J

jbruno4000

Looking at the way you're wanting to use the variable, perhaps you'll be better
off using a string variable instead of char. i.e.
#include <string>. You'll them be able to do what you seem to have in mind.

string name;

name = "modem";

and you can still access the individual components of name:

for(int count = 0; count < strlen(name); count++)
cout << name[count] << "," ;
cout << endl;

outputs: m, o, d, e, m,
 
D

Dave

John Carson said:
It is not the chars that are const --- if they were you couldn't change the
char values. Rather, it is the location in memory of the chars that cannot
be changed. In pointer terms, it is like a const pointer to char, i.e.,
char * const

2.13.4-1: An ordinary string literal has type "array of n const char"

And indeed, you may not change the characters of a string literal.
 
R

rajanikanth

I am trying to declare a string variable as an array of char's. the
code looks like this.

char name[80];

then when i try to use the variable it dosn't work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";


thanks for any help you can give me.

hi!

It is better to use the string class

#include<string>

string s = "modem";
cout << s << "\n";

Best,

Raj
 
A

Andrey Tarasevich

John said:
It is not the chars that are const --- if they were you couldn't change the
char values. Rather, it is the location in memory of the chars that cannot
be changed. In pointer terms, it is like a const pointer to char, i.e.,
char * const

I think Dave was talking about the literal. String literal is an "array
of const char" and its 'char' values cannot be changed.
 
J

John Carson

Andrey Tarasevich said:
I think Dave was talking about the literal. String literal is an
"array
of const char" and its 'char' values cannot be changed.


You are right. I didn't read the post carefully enough.
 
J

John Carson

Dave said:
2.13.4-1: An ordinary string literal has type "array of n const char"

And indeed, you may not change the characters of a string literal.


My mistake. I misread your post as referring to the array

char name[80];

rather than to the string literal.
 
D

Dave

John Carson said:
You are right. I didn't read the post carefully enough.

Although I think we've long since answered the OPs question, I'm now
starting to wonder about the accuracy of another part of my post.
Specifically, I'm wondering about where I stated the array is converted to a
pointer. Though such a conversion is one of the implicit conversions
described by the Standard, *does* the conversion happen here? Would there
be any reason for it to? After all, it's not as if we're trying to match
the array to a pointer parameter of a function, or some other such case
where a conversion is *needed*. Here, the assignment of an array (a string
literal) to another array is illegal to start with, so why would a
conversion happen only to come up with another construct (the assignment of
a pointer to an array) that is still just as illegal? So, I may have been
incorrect in that portion of my post. Can anybody confirm or deny this
authoritatively? I'd like to know for sure now that I've thought about it a
little longer...
 
J

Jeff

Hi there,

a better idea would almost certainly be for you to use std::string, as
it takes care of expanding the array for you (how can you be sure that
name[] will always be <79 chars?):

#include <string>
using std::string;

string name;
name = "modem"; // string expands automatically
name = "sfasfasdfasdf;asdf;kasdjf;aslkdfa;ksldjfas;dlfkj"; // ditto

with std::string, you can also easily do string copying:
std::string mycopy = name;
If you were using raw char arrays, you'd have to do strcpy(), etc ...
std::string is just an easier (clearer and better) way to do it.

PS if you need a C-style char* of your string, you can just use the
..c_str() member function.
 
A

Andrew Koenig

I am trying to declare a string variable as an array of char's. the
code looks like this.
char name[80];

If you want a string, use a string:

#include <string>

...

std::string name;

Whoever suggested using arrays of characters as strings gave you bad advice.
If the advice came from a book from which you are learning C++, I suggest
you try a different book.
 
G

Gary Labowitz

Andrew Koenig said:
I am trying to declare a string variable as an array of char's. the
code looks like this.
char name[80];

If you want a string, use a string:

#include <string>

...

std::string name;

Whoever suggested using arrays of characters as strings gave you bad advice.
If the advice came from a book from which you are learning C++, I suggest
you try a different book.

I should think using char [ ] for strings must still be taught. Firstly, it
is an intuitive way to "see" arrays and learn to use them. Secondly, there
is lots of code extant that uses them and a beginner better learn to
understand it. Thirdly, it still seems easier to me to teach it first and
then switch to standard string class. The relief or not having to deal with
the problems of char [ ] shows the benefit of the standard libraries
clearly. But making the switch also involves discussion of namespaces which
I tend to defer until the little coders are reasonably confident using the
scope resolution operator to access globals, statics, and classes.

Without knowing what book, I'd say it isn't the best advice to abandon it
just yet. I'm aware you prefer to teach standard C++ from the outset and
fill in the retrograde topics later. Besides, I can't get the school to use
your books -- damned committees!
 
A

Andrew Koenig

I should think using char [ ] for strings must still be taught. Firstly,
it
is an intuitive way to "see" arrays and learn to use them. Secondly, there
is lots of code extant that uses them and a beginner better learn to
understand it. Thirdly, it still seems easier to me to teach it first and
then switch to standard string class. The relief or not having to deal with
the problems of char [ ] shows the benefit of the standard libraries
clearly. But making the switch also involves discussion of namespaces which
I tend to defer until the little coders are reasonably confident using the
scope resolution operator to access globals, statics, and classes.

I agree with you that every professional C++ programmer must eventually
learn how to use arrays. However, I don't think that teaching them before
teaching strings is the most useful way to go. The trouble is that
beginners have enough information to absorb that there's a very good chance
that they will be unable to write programs that work correctly for quite a
while--long enough for them to become frustrated.

I have taught C++ both ways, and my experience is that the students learn a
lot faster when they start with strings, then move to arrays of characters.
One reason is that they spend much less time debugging.
Without knowing what book, I'd say it isn't the best advice to abandon it
just yet. I'm aware you prefer to teach standard C++ from the outset and
fill in the retrograde topics later. Besides, I can't get the school to use
your books -- damned committees!

Interesting. I'd like to know what the committees' objections are. Perhaps
we can discuss this offline?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top