C 访问冲突写入位置

标签 c exception

我刚刚编译了这段代码,它向我显示了这个错误:

Exception thrown at 0x0F2FC4DA (ucrtbased.dll) in Sample7.exe: 0xC0000005: Access violation reading location 0x97979436.

我根本不知道这个错误意味着什么,因为我刚刚使用 C 几个月。我还尝试在其他网站上寻求帮助,但没有找到任何帮助。

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int i = 0, n, length = 0;
    char *abc;
    printf("\n Enter size of array: ");
    scanf_s("%d", &n);
    abc = new char[n];
    printf("\n Enter symbols: ");
    scanf_s("%s", abc);
    length = strlen(abc);
    for (i = 0; i <= n; i++)
    {
        printf("\n Your array: ", abc);
        while (length = 10)
        {
            if (abc[i] >= 'A' && abc[i] <= 'Z')
            {
                abc[i] = ' ';

            }
            printf("\n Your array after deleting A-Z symbols",abc);
        }

    }
    delete[]abc;
    _getch();
    return 0;

最佳答案

您收到此错误是因为您访问的内存超出了已分配的空间。我的猜测是您正在访问其边界之外的 char 数组的索引。您需要逐行调试代码以查看发生这种情况的位置。

第一次查看您的代码时,我发现了以下错误。

for (i = 0; i <= n; i++) 我想你的意思是: for (i = 0; i < n; i++)

还有while (length = 10)应该是while (length == 10)

关于C 访问冲突写入位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47365133/

相关文章:

面临异常时构造函数中的c++空闲内存分配

java - Android 异常 NameNotFoundException

Java servlet 返回 404 错误

在 JPanel 中实现 Runnable 时出现 java.lang.StackOverflowError - Java Swing

c - 如何在C中使用数组将字符串存储在堆栈中?

c - newlib-nano printf 在嵌入式项目中转换为 iprintf

c++ - Apache 权限被拒绝连接到套接字

java - "java.net.NoRouteToHostException: Cannot assign requested address"是什么?

c - 在 C 中使用 getopt_long 的文件路径无效

c - POSIX 套接字 : How to detect Ctrl-C sent over Telnet?