LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-09-2012, 05:40 AM   #1
peacepanda
LQ Newbie
 
Registered: Oct 2012
Posts: 26

Rep: Reputation: Disabled
Question Question with c++,string and arrays?


I want your opinions and also a solution for my underlying problem if possible.

Here is the problem.
I have a structure say
Quote:
struct Property
{
char name[100];
char value[100];
};
I want to execute a couple of looping statements like this

strcpy(user_property[0].name,"value1");
strcpy(user_property[0].value,((const char*)Getvalue1()));

Here Getvalue1() is a memeber function of a class that returns a string value I have to actually run a loop to assign 15 such values.This can be done if I pass array indexes so first I have to store the values value1 etc and (const char*)Getvalue1() in some array but I am not sure how to do this I tried doing the same but I have the following compilation errors

This is the sample code that I have done
PHP Code:
struct Property  user_property[2];
        
my_Class response;
        
response.SetCode("Ajit");
        
response.SetMQCode("Gunge");
        
string sample xmlrMQresponse.GetMQErrorCode();
        
string sample1 xmlrMQresponse.GetMQStateCode();

        const 
char* const list1[] = {"stateCode","errorCode"};
        const 
char* const list2[] = {sample1};
        const 
size_t len sizeof(list1) / sizeof(list1[0]);
        for (
size_t i 0<leni++) {
            
strcpy(user_property[i].name,list1[i]);
            
strcpy(user_property[i].value,list2[i]);
        } 
The compile error I am getting is

PHP Code:
connot convert std::string to const char in initialization 

at line
PHP Code:
const char* const list2[] = {sample1}; 
Can someone help me with the resolution here?

Regards,
Ajit
 
Old 10-09-2012, 05:50 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Use "sample1.c_str()" instead of just "sample1". c_str() returns a pointer to a char buffer representing the underlying string - it's guaranteed not to change so long as you only use const methods on the string (if I remember correctly).

Also, consider abandonning strcpy() and using strncpy(), it's safer (i.e. will save you headaches later on).
 
1 members found this post helpful.
Old 10-09-2012, 06:51 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,522

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
@ the OP,

Unless you are developing real-time code, I would also recommend abandoning character arrays for storing strings. Just store them within an STL string.

Code:
struct Property
{
    std::string name;
    std::string value;
};
...

Property user_property[2];    // no need to specify struct in declaration

std::string sample  = "mqErrorCode";
std::string sample1 = "mqStateCode";

const std::string list1[] = { "stateCode", "errorCode" };
const std::string list2[] = { sample1 };
const size_t len = sizeof(list1) / sizeof(list1[0]);

for (size_t i = 0; i < len; ++i)
{
    user_property[i].name  = list1[i];
    user_property[i].value = list2[i];    // BUG HERE!  list2 only has one item, not two.
}
 
1 members found this post helpful.
Old 10-16-2012, 04:52 AM   #4
peacepanda
LQ Newbie
 
Registered: Oct 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
Smile Thanks

Hi dwhitney67 and dwhitney67 thanks for your responses.That helps.

Ajit
 
  


Reply

Tags
c++, strings


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] I got a question about inserting char arrays into a string... trist007 Programming 1 04-20-2011 06:21 PM
Structures and String Arrays, C SithArrow Programming 4 04-18-2011 10:32 PM
[SOLVED] Accessing arrays position in a split string xeon123 Linux - Newbie 4 03-10-2011 09:36 AM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM
C Question: arrays and pointers tundra Programming 7 07-19-2002 03:38 AM


All times are GMT -5. The time now is 06:47 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration