473,545 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I need some basic C++ help

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.
Jul 22 '05 #1
41 3907

"Psykarrd" <ps******@hotma il.com> wrote in message
news:19******** *************** ***@posting.goo gle.com...
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!
Jul 22 '05 #2
Psykarrd wrote:
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);

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #3

"Dave" <be***********@ yahoo.com> wrote in message
news:vr******** ****@news.super news.com...

"Psykarrd" <ps******@hotma il.com> wrote in message
news:19******** *************** ***@posting.goo gle.com...
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...
Jul 22 '05 #4

"Psykarrd" <ps******@hotma il.com> wrote in message
news:19******** *************** ***@posting.goo gle.com...
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
Jul 22 '05 #5
"Dave" <be***********@ yahoo.com> wrote in message
news:vr******** ****@news.super news.com

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
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #6

"John Carson" <do***********@ datafast.net.au > wrote in message
news:3f******@u senet.per.parad ox.net.au...
"Dave" <be***********@ yahoo.com> wrote in message
news:vr******** ****@news.super news.com

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
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


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.
Jul 22 '05 #7
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,

Jul 22 '05 #8
ps******@hotmai l.com (Psykarrd) wrote in message news:<19******* *************** ****@posting.go ogle.com>...
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
Jul 22 '05 #9
John Carson wrote:
"Dave" <be***********@ yahoo.com> wrote in message
news:vr******** ****@news.super news.com

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


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

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #10

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

Similar topics

2
4220
by: AK | last post by:
I don't want any part of the previous discussion on Visual Basic versus Visual Basic.Net. My query is about using Visual Basic for Applications; and whether it is better to use Visual Basic 6 or Visual Basic.Net as a springboard for studying VBA. I use Office 2000 and would like to use VBA as a tool to customize it. I have zero...
4
2423
by: Steve Richfie1d | last post by:
I wrote my first BASIC compiler when Bill Gates was going to Lakeside School, and am believed to be the original inventor of the ON ERROR statement. Now, my son wants to learn Visual Basic, but NOT from me. He wants a manual that is NOT a handholding tome on how to write a program, but instead is rather a complete language description in...
9
2913
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with a device. The definition of the table is as follows: CREATE TABLE devicedata ( device_id int NOT NULL REFERENCES devices(id), -- id in the...
1
2128
by: Tom Rahav | last post by:
Hello all! I develop application in Visual Basic .NET and ORACLE database. My question is how do I "send" script file to the database using visual basic .net. Other words, is there any way to send to the database a script file to run, as done by: "@FullPath\FileName" in SQL PLUS ? If not, then I need your help with something else: I tried...
13
3008
by: usenet | last post by:
How and where can one find out about the basics of VB/Access2003 syntax? I am a died in the wool C/C++/Java Linux/Unix programmer and I am finding it difficult to understand the program format for accessing objects, controls, etc. in VB/Access2003. In particular where will I find explanations of:- Actions, Functions, Methods,...
4
2194
by: robinsand | last post by:
My apologies to those of you who are more advanced Visual C++ .NET programmers, but I am working on a project for an MBA course that is condensed into an eight-week schedule, and I need help getting a program up and running with proper files and documentation to be handed in for a grade (on Microsoft Visual Studio .NET 2003). I am being...
0
883
CLAW
by: CLAW | last post by:
Hi, I'm new with Visual Basic. I've started a Web Browser project in Visual Basic 2005 Express Edition, but being new I really need some help: 1) When the user presses a button, I want to execute an help file in the same directory as the browser, but before to check if the file exists. How can I do that? I also want to execute the Internet...
51
6482
by: Steven Spits | last post by:
Hi, Plannig to buy Vista, but not sure what version to get. I do VS.NET development, mostly ASP.NET. Can IIS be installed on Vista home premium? Or do I need business or ultimate? Steven
4
1382
by: ooroboo | last post by:
i wont a text file to load into a rich text on box on load , then save when the form closes. my problem has been creating the text file , i am very new for visual basic and could use some help this is my attempt on this. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ...
3
1324
by: shimul | last post by:
Hi All, Have query where I get the result like (except "-------------------",put it to separate the two columns) NUMBER ---------------TYPE DESCRIPTION 17 --------------------------Basic 17 --------------------------Basic 17 --------------------------Actions 17 ---------------------------Conditions 17...
0
7465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7752
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.