473,839 Members | 1,465 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 3987

"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
4254
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 programming experience. I would like to read through and work on the examples of a beginners
4
2438
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 the tradition of the old IBM language manuals. Does this exist? Is there anything like this...
9
2944
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 device
1
2157
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 to send to the database the script file's content, in order to run the commands in it (create...
13
3046
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, Properties - I'm understand the
4
2217
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 graded on how well I incorporate advanced C++ features such as inheritance, polymorphic programming,...
0
896
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 Properties form (C:\WINDOWS\system32\inetcpl.cpl for WinXP) 2) Need some help with the progress bar. I...
51
6534
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
1397
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 RichTextBox1.LoadFile("C:\OutputFolder\lol", RichTextBoxStreamType.PlainText) On Error Resume Next...
3
1338
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 ----------------------------Factors
0
9855
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10586
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10293
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7017
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5682
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5866
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3134
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.