0

In main.cpp I must print my char array like this:

const char *str(CValue::TestStringValue0());
cout << ' ' << *str << endl; //must not change

I can't modify this! So I need to print my array of char, but nor first value of array.

TestStringValue look like this..

static const char* TestStringValue0() {...}
6
  • Don't understand the question Commented Nov 21, 2013 at 19:19
  • Can you show the code for TestStringValue0()? Instead of returning e.g. str you would return str+1 Commented Nov 21, 2013 at 19:19
  • What does CValue::TestStringValue0 look like? And what is your question? Commented Nov 21, 2013 at 19:23
  • Now I'm having it: static const char* TestStringValue0() '{ return "0,0,0"; }' I must print "0,0,0", not "0" Commented Nov 21, 2013 at 19:25
  • Are you asking how to make code that prints a single character magically print a string without changing it? There's no sensible way to do that, and probably no crazy way either. Or have I misunderstood? Commented Nov 21, 2013 at 19:28

2 Answers 2

1

I'm not sure I understand you correctly, but if you're not supposed to modify this line:

cout << ' ' << *str << endl; //must not change

Then you can do like this:

const char *tempStr(CValue::TestStringValue0());
const char **str = &tempStr;
cout << ' ' << *str << endl; //must not change

In this case cout will print the whole string returned from TestStringValue0, while in the original code it printed only the first char.

P.S. What a strange condition you have :)

Sign up to request clarification or add additional context in comments.

2 Comments

Why do you need a temporary variable?
@andre, since we can't modify the line with count, we need to make *str* a pointer, so we need str` to be a pointer to a pointer. As for the additional variable, I guess, I could avoid it, but I don't feel comfortable with getting address of values just returned from a function :) It's a matter of habit.
1
const char *str(CValue::TestStringValue0());
const char *p = str;

for ( ; *str; ++str )
{
    cout << ' ' << *str << endl; //must not change
}

:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.