Error text on console screen

hello i got a problem , error text on console .

Linux debian 6.06 in virtual box , the IDE is codeblocks

the pict

[IMG]http://i.imgur.com/U2ZWc.png[/IMG]


the pict when i`m using windows , IDE is codeblocks

[IMG]http://i.imgur.com/xC6zF.jpg[/IMG]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
    char s[25];
    char* d;
    d = "You are my dreams ";

    strcpy(s,d);
    cout << s;
    cout << " Hellow" << endl;
    return 0;
}

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>
using namespace std;

int main(int argc,char** args){
char s[25];
char* d=new char;

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 const char*.

@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?
Last edited on
@peter87 that's same code on both systems ...

@alea & @halit ....thanks . it's solve :D

.....
i see ..
in linux , there are a little different things
Last edited on
thank you to you:)
frytr
I hope you didn't use halitciftcise's solution. It's corrupts the heap and is bad advice. Go with Peter87's advice.
Topic archived. No new replies allowed.