×

# Why is this program not working as expected ?

 0 I am trying to reverse a string without affecting the position of special characters in it. But, my program is not running properly and it gives a runtime error when executed on Ideone - link. From what I can think is that there is some problem with the char* variable that I am using there. Please correct me and provide an explanation why this is happening. THANKS IN ADVANCE Here's the code if you wish to see here #include using namespace std; char *func(char*); int main(){ char* in = "ab$cd"; char* out = ""; out = func(in); printf("%s" , out); return 0; } char* func(char* in){ //char *out = ""; int len = strlen(in); int i = 0; int j = len - 1; while(i  2 I got this list of errors on running this at hackerrank compiler- Runtime error Compile Time Compile Message solution.cc: In function ‘int main()’: solution.cc:8:12: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] char* in = "ab$cd"; ^~~~~~~ solution.cc:10:13: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]  I think you should google this thing up. Sites like Stackoverflow should have something related to it. EDIT- Link answered 24 Apr '17, 02:42 15.4k●1●20●65 accept rate: 18% 1 @vijju123 Thanks for answering. Actually I did google it but couldn't find anything worth and I find codechef community better than SO community that's why I always come here first. (24 Apr '17, 03:06) That's true dear. Also, I don't expect google to yield results without knowing the specific error. Once we get hold of the name, then things get easy :) (24 Apr '17, 03:18)
 2 I think this link explains the issue satisfactorily. Assigning a string to "char *in" prevents you from modifying it in the future. Use "char in[]" instead. answered 24 Apr '17, 02:47 6★meooow ♦ 7.1k●7●18 accept rate: 48% Yes, that's what my link says too. Thanks @meooow (24 Apr '17, 02:49) 1 Yes, actually we posted our links almost simultaneously, so I only saw yours after the page refreshed :) (24 Apr '17, 02:54) meooow ♦6★
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×2,714
×1,911
×640
×518

question asked: 24 Apr '17, 02:18

question was seen: 392 times

last updated: 24 Apr '17, 03:18