小编pgp*_*pgp的帖子

int const array cannot be written to

Compiling the following program

int main(void) {
    int const c[2];
    c[0] = 0;
    c[1] = 1;
}
Run Code Online (Sandbox Code Playgroud)

leads to error: assignment of read-only location ‘c[0]’. As I understand it, the const only applies to the location of c and so c[0] and c[1] should be mutable. Why is this error produced?

c constants

2
推荐指数
1
解决办法
61
查看次数

写入字符串数组时出现分段错误

我正在尝试将 argv 中传递的一些参数复制到字符串数组中。这是我的程序。

#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main (int argc, char * argv[]) {
    char **args = malloc(argc * sizeof(char *));
    for (int i = 0; i < argc - 1; ++i) {
        strcpy(*(args+i), argv[i+1]);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在 for 循环中遇到分段错误。为什么会这样?

c segmentation-fault

0
推荐指数
1
解决办法
49
查看次数

标签 统计

c ×2

constants ×1

segmentation-fault ×1