I am getting this warning when i compile your source file:
warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
As far as i know the problem is that "You are my dreams " is a unnamed string constant (i.e. there is no variable which you can use to access the string). I guess the memory that is needed to store the string constant is de-allocated right after line 11 in your code, which results in unpredictable behaviour of your program (depending on compiler).
To solve this problem you can omit the char* d variable:
1 2 3 4 5 6 7 8 9
int main()
{
char s[25];
strcpy(s, "You are my dreams ");
cout << s;
cout << " Hellow" << endl;
return 0;
}
Hi, i want to give an idea . you must use dynamic variable for "d" . Becaus you use this variable in this section which is strcpy(d,"You are my dreams ")
program output. >
You are my dreams Hellow
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<iostream>
#include<cstring>
usingnamespace std;
int main(int argc,char** args){
char s[25];
char* d=newchar;
strcpy(d,"You are my dreams ");
strcpy(s,d);
cout<<s;
cout<<" Hellow"<<endl;
return 0;
}
@AleaIactaEst
The string is not deallocated. It stays there for the whole run of the program. The warning you get is because he uses a non-const pointer to point to data that should not be modified. The correct thing to do is to make d a constchar*.
@halitciftcise
He doesn't need to use dynamic allocation. If you want to use dynamic allocation you should at least allocate an array that is big enough to hold the string. Now you are only allocating a single char.
@RyuKnightly
I don't see why you would get that output. Are you sure you use the exact same code on both systems?