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. 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!
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
"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...
"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
"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)
"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.
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, 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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
|
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
...
|
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...
|
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...
| |
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...
|
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. ...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |