hello,
i have this small little program where i would like to change the contents of a char* variable in another file.
#include <windows.h>
#include "somefile.h"
char* sometext = "hello world";
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
//before this line sometext contains hello world
changetext();
//after this line sometext contains gibberish
return 0;
}
the somefile.h looks like this
#pragma once
#include <windows.h>
#include <string>
using namespace std;
extern char* sometext;
void changetext() {
string s = "a new text";
//before this line sometext contains hello world
sometext = (char*) s.c_str();
//after this line sometext contains a new text
}
why is the "a new text" string not correctly given back to the main function?
thanks!