我在我的工程中需要使用一个全局字符指针变量,但是声明后在函数中进行对
此变量进行更改的时候总是出现错误。
char * myChar="This is my string data.";void myfunction()
{
...
myChar= "This is my new string."; // 程序执行到此出现错误;
...
}

解决方案 »

  1.   

    I am waiting for u...
      

  2.   

    如果只是这样写是没有错的,错的怕是你有myChar[?]='?'类似的写操作,由于在函数的那个myChar是指向常量"This is my string data.",所以不能对它进行写操作,在函数内部myChar被指向另一个常量"This is my new string.",也不能对它进行写操作.但是读操作是可以的,更何况在你列出的程序根本是没有操作.
      

  3.   

    短信收到.D & Q 方法:你只需将
    char * myChar="This is my string data.";
    更改为
    char myChar[]="This is my string data.";
    即可.但是你不得使用超过strlen("This is my string data.")长度.void myfunction()
    {
    ...
    myChar= "This is my new string."; // 程序执行到此出现错误;
    ...
    }
    需改为
    void myfunction()
    {
    ...
      strcpy(myChar, "This is my new string.");
    ...
    }标准方法:char* myChar=NULL;void main()
    {
       myChar=new char[1024];
       if(!myChar)
          return;   strcpy(myChar,"This is my string data.");   ....   delete []myChar;
    }void myfunction()
    {
    ...
      strcpy(myChar, "This is my new string.");
    ...
    }
      

  4.   

    虽然可以使用,
    但是在我的DLL中还是会出错。
    在EXE工程中使用没有错误,
    怎么回事?
      

  5.   

    这可能是因为EXE和其调用的DLL中使用动态内存申请和释放不配套造成的.如在VC6下,建议将new改为HeapAlloc().