Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

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
struct Property
char name[100];
char value[100];
I want to execute a couple of looping statements like this

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;
string sample xmlrMQresponse.GetMQErrorCode();
string sample1 xmlrMQresponse.GetMQStateCode();

char* const list1[] = {"stateCode","errorCode"};
char* const list2[] = {sample1};
size_t len sizeof(list1) / sizeof(list1[0]);
        for (
size_t i 0<leni++) {
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?

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

Rep: Reputation: 139Reputation: 139
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
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
@ 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.

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
LQ Newbie
Registered: Oct 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
Smile Thanks

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



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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:30 AM.

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