c++ - 为什么在指针操作中出现访问冲突异常?

标签 c++ .net-4.0

我在 Windows 7.0 x64 上使用 C++ Visual Studio .Net 4.0。这仅发生在 while 语句的第一个循环中。

int main()
{
  char *string = new char[11];

  string = "characters\0";

  toUppercase(string);

  return 0;
}



void toUppercase(char *stringPtr)
{
 while(*stringPtr != '\0')
 {
    if(*stringPtr >= 'a' && *stringPtr <= 'z')
    {
        *stringPtr = *stringPtr - 32; // this is the culprit
    }

    ++stringPtr;    
 }
}

最佳答案

我怀疑你正在做这样的事情:

toUppercase("test");

问题是"test"const char的数组,不是char,所以不能修改。然而,由于非常愚蠢的弃用特殊转换,字符串文字无论如何都可以被视为 char*

(您的函数也无法测试 toUppercase(0)。)

关于c++ - 为什么在指针操作中出现访问冲突异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5132579/

相关文章:

c++ - 如果我从不同的线程调用一个对象成员函数会发生什么?

C++ 组合最佳实践

c# - 使用 LINQ 读取带分隔符的文件

wpf - 在可视化树中移动 UIElement 而不重新计算布局

c# - 如何在 .NET 4 中使用 Console.CancelKeyPress? (在 .NET 3.5 及以下版本中工作正常)

c++ - boost::function 静态成员变量

C++ pthread 和 Qt

c++ - 使用指针表示法从矩阵读取数据

c# - 协方差也在 3.5/2.0 中?

c# - 我可以在 WPF 中以同步方式使用多个 BackgroundWorker 吗?