小编LSG*_*LSG的帖子

为什么要警告实现指向字符串的指针数组?

我试图使用指针实现一个字符串数组.以下是我的代码.

#include <stdio.h>
const int MAX = 4;
int main () {
char *names[] = {
  "abcd",
  "efgh",
  "ijkl",
  "mnop",
};
int i = 0;
for ( i = 0; i < MAX; i++) {
  printf("Value of names[%d] = %s\n", i, names[i] );
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在运行时发出警告.以下是警告:

C:\workspace>g++ test_pointer_string_arr.c -o tpsa.exe
test_pointer_string_arr.c: In function 'int main()':
test_pointer_string_arr.c:12:4: warning: deprecated conversion from string       const
ant to 'char*' [-Wwrite-strings]
};
^
test_pointer_string_arr.c:12:4: warning: deprecated conversion from string const
ant to 'char*' …
Run Code Online (Sandbox Code Playgroud)

c++ arrays gcc pointers string-literals

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

标签 统计

arrays ×1

c++ ×1

gcc ×1

pointers ×1

string-literals ×1