Ton*_*tes 7 c arrays gcc struct function
这是我的代码。
\n// test.c\n#include <stdio.h>\n\n#define ARRAY_SIZE 4\n\nstruct example_t {\n int field0;\n int field1;\n int field2;\n int field3;\n int field4;\n};\n\nstruct example_t func(int *in, int array[ARRAY_SIZE]) {\n struct example_t out;\n return out;\n}\n\nint main() {\n int array[ARRAY_SIZE] = { 0, 0, 0, 0 };\n int a = 0;\n struct example_t col = func(&a, array);\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\ngcc11.1.0 给了
$ gcc test.c -o test\ntest.c: In function \xe2\x80\x98main\xe2\x80\x99:\ntest.c:22:26: warning: \xe2\x80\x98func\xe2\x80\x99 accessing 16 bytes in a region of size 4 [-Wstringop-overflow=]\n 22 | struct example_t col = func(&a, array);\n | ^~~~~~~~~~~~~~~\ntest.c:22:26: note: referencing argument 2 of type \xe2\x80\x98int *\xe2\x80\x99\ntest.c:14:18: note: in a call to function \xe2\x80\x98func\xe2\x80\x99\n 14 | struct example_t func(int *in, int array[ARRAY_SIZE]) {\n | ^~~~\nRun Code Online (Sandbox Code Playgroud)\n但g++没有。
我不明白为什么会出现警告消息,因为我的代码中没有字符串操作,而且我从未array读过func.
如果 中只有 4 个或更少的字段struct example_t,GCC 不会抱怨。有人可以解释一下为什么这条消息在这里以及我该如何修复它吗?
先感谢您。
\n| 归档时间: |
|
| 查看次数: |
1183 次 |
| 最近记录: |