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?
我正在尝试将 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 循环中遇到分段错误。为什么会这样?