【问题标题】:gdb: <error reading variable> in array while debugging VS codegdb:调试VS代码时数组中的<错误读取变量>
【发布时间】:2021-02-27 03:06:15
【问题描述】:

在调试期间尝试查看数组的内容时遇到问题。而不是我看到的字符。

我的代码:

#include <stdio.h>

int main()
{
    char str[100] = {0};
    fgets(str, 100, stdin);

    return 0;
}

我在VARIABLES 窗口中看到的内容:

但是当我尝试调试 int 数组时,一切正常。

示例:

#include <stdio.h>

int main()
{
    int str[100];
    for (int i = 0; i < 100; i++)
    {
        str[i] = i+1;
    }
    

    return 0;
}

我所看到的:

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe\""
        }
    ]
}

launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc.exe build active file"
        }
    ]
}

如何查看数组中的字符?

【问题讨论】:

  • 我也遇到了同样的问题,你找到解决办法了吗?

标签: arrays c debugging visual-studio-code char


【解决方案1】:

因为漂亮的打印机没有正确显示 utf-8 字符。要显示 utf-8 字符,请编辑 launch.json 中的 setupCommands:

  "setupCommands": [
    {
      "description": "Enable pretty-printing for gdb",
      "text": "-enable-pretty-printing",
      "ignoreFailures": true
    },
    {
      "description": "Fix pretty-printing for gdb",
      "text": "set charset UTF-8"
    }
  ],

原答案:VSCode debugger having issues with character encoding

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-08
    • 2016-01-12
    • 2018-07-22
    • 2012-04-18
    • 2018-11-20
    • 2020-10-01
    • 2022-11-05
    相关资源
    最近更新 更多