鱼C论坛

 找回密码
 立即注册
查看: 3504|回复: 24

[已解决]*a[]

[复制链接]
发表于 2018-2-24 11:05:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
为什么小于17的数,下边的 printf 都不显示东西呢,大于17就可以
最佳答案
2018-2-24 16:09:15
哥斯拉不说话 发表于 2018-2-24 16:03
还是原来的代码,改成 %ld 之后,什么警告都没有了,编译运行,都没问题啊,还是不清楚那个 *a[] 里应该 ...
  1. #include <stdio.h>

  2. typedef unsigned char uint8_t;

  3. int main(void)
  4. {
  5.         uint8_t *a[17];

  6.         for(int i = 0; i < 17; ++i)
  7.                 a[i] = "hello world!";

  8.         for(int i = 0; i < 17; ++i)
  9.                 *(a + i) = "hello world!";

  10.         *(a + 0) = "hello world!";
  11.         *(a + 16) = "hello world!";

  12.         return 0;
  13. }
复制代码


*(a + 0) = "hello world!";   // 数组的第0个空间
*(a + 16) = "hello world!"; // 数组最后一个空间
code.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-2-24 13:03:31 | 显示全部楼层
贴代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-24 14:15:22 | 显示全部楼层
  1. #include <stdio.h>

  2. typedef unsigned char uint8_t;

  3. int main(void)
  4. {
  5.         uint8_t *a[17]; // 这里为什么是 17 呢?小于17的数都不行
  6.        
  7.         printf("%d, %d, %d, %d\n", sizeof(a), sizeof(*a), sizeof(uint8_t), sizeof((uint8_t *)"00000000"));
  8.        
  9.         *a = (uint8_t *)"00000000";
  10.         *(a+8) = (uint8_t *)"11111111";
  11.         *(a+16) = (uint8_t *)"22222222";
  12.         *(a+24) = (uint8_t *)"33333333";
  13.         *(a+32) = (uint8_t *)"44444444";
  14.         *(a+40) = (uint8_t *)"55555555";
  15.         *(a+48) = (uint8_t *)"66666666";
  16.        
  17.         printf("%s\n", *a);
  18.         printf("%s\n", *(a+8));
  19.         printf("%s\n", *(a+16));
  20.         printf("%s\n", *(a+24));
  21.         printf("%s\n", *(a+32));
  22.         printf("%s\n", *(a+40));
  23.         printf("%s\n", *(a+48));
  24.        
  25.         return 0;
  26. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-24 14:24:35 | 显示全部楼层
17也不行

  1. #include <stdio.h>

  2. typedef unsigned char uint8_t;

  3. int main(void)
  4. {
  5.         uint8_t *a[17]; // 这里为什么是 17 呢?小于17的数都不行

  6.         printf("%d, %d, %d, %d\n", sizeof(a), sizeof(*a), sizeof(uint8_t), sizeof((uint8_t *)"00000000"));

  7.         *a = (uint8_t *)"00000000";
  8.         *(a + 8) = (uint8_t *)"11111111";
  9.         *(a + 16) = (uint8_t *)"22222222";
  10.         *(a + 24) = (uint8_t *)"33333333";
  11.         *(a + 32) = (uint8_t *)"44444444";
  12.         *(a + 40) = (uint8_t *)"55555555";
  13.         *(a + 48) = (uint8_t *)"66666666";

  14.         printf("%s\n", *a);
  15.         printf("%s\n", *(a + 8));
  16.         printf("%s\n", *(a + 16));
  17.         printf("%s\n", *(a + 24));
  18.         printf("%s\n", *(a + 32));
  19.         printf("%s\n", *(a + 40));
  20.         printf("%s\n", *(a + 48));

  21.         return 0;
  22. }
复制代码

  1. 1>------ 已启动全部重新生成: 项目: tmp, 配置: Debug Win32 ------
  2. 1>main.c
  3. 1>c:\visualstudioprojects\tmp\tmp\main.c(14): warning C4789: 缓冲区“a”(大小为 68 字节)将溢出;4 字节将在偏移 96 时开始写入
  4. 1>c:\visualstudioprojects\tmp\tmp\main.c(15): warning C4789: 缓冲区“a”(大小为 68 字节)将溢出;4 字节将在偏移 128 时开始写入
  5. 1>c:\visualstudioprojects\tmp\tmp\main.c(16): warning C4789: 缓冲区“a”(大小为 68 字节)将溢出;4 字节将在偏移 160 时开始写入
  6. 1>c:\visualstudioprojects\tmp\tmp\main.c(17): warning C4789: 缓冲区“a”(大小为 68 字节)将溢出;4 字节将在偏移 192 时开始写入
  7. 1>tmp.vcxproj -> C:\VisualStudioProjects\tmp\Debug\tmp.exe
  8. 1>已完成生成项目“tmp.vcxproj”的操作。
  9. ========== 全部重新生成: 成功 1 个,失败 0 个,跳过 0 个 ==========
复制代码

  1. #include <stdio.h>

  2. typedef unsigned char uint8_t;

  3. int main(void)
  4. {
  5.         uint8_t *a[49]; // *(a + 48) = (uint8_t *)"66666666"; 所以这里需要49个

  6.         printf("%d, %d, %d, %d\n", sizeof(a), sizeof(*a), sizeof(uint8_t), sizeof((uint8_t *)"00000000"));

  7.         *a = (uint8_t *)"00000000";
  8.         *(a + 8) = (uint8_t *)"11111111";
  9.         *(a + 16) = (uint8_t *)"22222222";
  10.         *(a + 24) = (uint8_t *)"33333333";
  11.         *(a + 32) = (uint8_t *)"44444444";
  12.         *(a + 40) = (uint8_t *)"55555555";
  13.         *(a + 48) = (uint8_t *)"66666666";

  14.         printf("%s\n", *a);
  15.         printf("%s\n", *(a + 8));
  16.         printf("%s\n", *(a + 16));
  17.         printf("%s\n", *(a + 24));
  18.         printf("%s\n", *(a + 32));
  19.         printf("%s\n", *(a + 40));
  20.         printf("%s\n", *(a + 48));

  21.         return 0;
  22. }
复制代码

  1. 1>------ 已启动全部重新生成: 项目: tmp, 配置: Debug Win32 ------
  2. 1>main.c
  3. 1>tmp.vcxproj -> C:\VisualStudioProjects\tmp\Debug\tmp.exe
  4. ========== 全部重新生成: 成功 1 个,失败 0 个,跳过 0 个 ==========
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-24 14:25:41 | 显示全部楼层
  1. #include <stdio.h>

  2. typedef unsigned char uint8_t;

  3. int main(void)
  4. {
  5.         uint8_t *a[17];

  6.         for(int i = 0; i < 17; ++i)
  7.                 a[i] = "hello world!";

  8.         for(int i = 0; i < 17; ++i)
  9.                 *(a + i) = "hello world!";

  10.         *(a + 0) = "hello world!";
  11.         *(a + 16) = "hello world!";

  12.         return 0;
  13. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-24 15:16:52 | 显示全部楼层

为什么我的没有警告也没有报错呢,我用的是 gcc,难道是 gcc 的问题?
code.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-24 15:20:54 | 显示全部楼层
哥斯拉不说话 发表于 2018-2-24 15:16
为什么我的没有警告也没有报错呢,我用的是 gcc,难道是 gcc 的问题?

  1. sh-4.4$ gcc -Wall main.c
  2. main.c: In function 'main':
  3. main.c:9:16: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [-Wformat=]
  4.          printf("%d, %d, %d, %d\n", sizeof(a), sizeof(*a), sizeof(uint8_t), sizeof((uint8_t *)"00000000"));
  5.                 ^
  6. main.c:9:16: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
  7. main.c:9:16: warning: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Wformat=]
  8. main.c:9:16: warning: format '%d' expects argument of type 'int', but argument 5 has type 'long unsigned int' [-Wformat=]
  9. main.c:9:16: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [-Wformat=]
  10. main.c:9:16: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
  11. main.c:9:16: warning: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Wformat=]
  12. main.c:9:16: warning: format '%d' expects argument of type 'int', but argument 5 has type 'long unsigned int' [-Wformat=]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-24 15:30:40 | 显示全部楼层

我用了 -Wall 也没有这些警告
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-24 15:31:17 | 显示全部楼层
哥斯拉不说话 发表于 2018-2-24 15:30
我用了 -Wall 也没有这些警告

截图看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-24 15:33:36 | 显示全部楼层

这是截图
code.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-24 15:43:22 | 显示全部楼层

你的gcc是从哪里安装的?
cygwin ?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-24 15:48:23 | 显示全部楼层
人造人 发表于 2018-2-24 15:43
你的gcc是从哪里安装的?
cygwin ?

电脑是 xp 的,在百度上搜索了 “xp 32位gcc 下载”,然后随便下了一个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-24 15:50:50 | 显示全部楼层
人造人 发表于 2018-2-24 15:43
你的gcc是从哪里安装的?
cygwin ?

这个是 gcc -v 的输出
code.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-24 15:56:48 | 显示全部楼层

换一个gcc试试
用cygwin试试

这是我的gcc
  1. sh-4.4$ gcc -v
  2. Using built-in specs.
  3. COLLECT_GCC=gcc
  4. COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/lto-wrapper.exe
  5. Target: x86_64-pc-cygwin
  6. Configured with: /cygdrive/i/szsz/tmpp/gcc/gcc-5.4.0-1.x86_64/src/gcc-5.4.0/configure --srcdir=/cygdrive/i/szsz/tmpp/gcc/gcc-5.4.0-1.x86_64/src/gcc-5.4.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --libexecdir=/usr/lib --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --enable-__cxa_atexit --with-dwarf2 --with-tune=generic --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libcilkrts --enable-libgomp --enable-libitm --enable-libquadmath --enable-libquadmath-support --enable-libssp --enable-libada --enable-libgcj-sublibs --disable-java-awt --disable-symvers --with-ecj-jar=/usr/share/java/ecj.jar --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible
  7. Thread model: posix
  8. gcc version 5.4.0 (GCC)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-24 16:01:52 | 显示全部楼层
人造人 发表于 2018-2-24 15:56
换一个gcc试试
用cygwin试试

printf 那里改成 %ld 就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-24 16:03:39 | 显示全部楼层
人造人 发表于 2018-2-24 15:56
换一个gcc试试
用cygwin试试

还是原来的代码,改成 %ld 之后,什么警告都没有了,编译运行,都没问题啊,还是不清楚那个 *a[] 里应该填多少
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-24 16:05:13 | 显示全部楼层
人造人 发表于 2018-2-24 15:56
换一个gcc试试
用cygwin试试

这是新换的 gcc
code.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-24 16:05:51 | 显示全部楼层
哥斯拉不说话 发表于 2018-2-24 16:03
还是原来的代码,改成 %ld 之后,什么警告都没有了,编译运行,都没问题啊,还是不清楚那个 *a[] 里应该 ...

问题是这个

  1. 1>------ 已启动全部重新生成: 项目: tmp, 配置: Debug Win32 ------
  2. 1>main.c
  3. 1>c:\visualstudioprojects\tmp\tmp\main.c(14): warning C4789: 缓冲区“a”(大小为 68 字节)将溢出;4 字节将在偏移 96 时开始写入
  4. 1>c:\visualstudioprojects\tmp\tmp\main.c(15): warning C4789: 缓冲区“a”(大小为 68 字节)将溢出;4 字节将在偏移 128 时开始写入
  5. 1>c:\visualstudioprojects\tmp\tmp\main.c(16): warning C4789: 缓冲区“a”(大小为 68 字节)将溢出;4 字节将在偏移 160 时开始写入
  6. 1>c:\visualstudioprojects\tmp\tmp\main.c(17): warning C4789: 缓冲区“a”(大小为 68 字节)将溢出;4 字节将在偏移 192 时开始写入
  7. 1>tmp.vcxproj -> C:\VisualStudioProjects\tmp\Debug\tmp.exe
  8. 1>已完成生成项目“tmp.vcxproj”的操作。
  9. ========== 全部重新生成: 成功 1 个,失败 0 个,跳过 0 个 ==========
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-24 16:07:19 | 显示全部楼层
哥斯拉不说话 发表于 2018-2-24 16:03
还是原来的代码,改成 %ld 之后,什么警告都没有了,编译运行,都没问题啊,还是不清楚那个 *a[] 里应该 ...

uint8_t *a[17];

a是数组,数组里面有17个元素(0~16),每个元素都是一个指针,uint8_t类型的指针
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-24 16:09:15 | 显示全部楼层    本楼为最佳答案   
哥斯拉不说话 发表于 2018-2-24 16:03
还是原来的代码,改成 %ld 之后,什么警告都没有了,编译运行,都没问题啊,还是不清楚那个 *a[] 里应该 ...
  1. #include <stdio.h>

  2. typedef unsigned char uint8_t;

  3. int main(void)
  4. {
  5.         uint8_t *a[17];

  6.         for(int i = 0; i < 17; ++i)
  7.                 a[i] = "hello world!";

  8.         for(int i = 0; i < 17; ++i)
  9.                 *(a + i) = "hello world!";

  10.         *(a + 0) = "hello world!";
  11.         *(a + 16) = "hello world!";

  12.         return 0;
  13. }
复制代码


*(a + 0) = "hello world!";   // 数组的第0个空间
*(a + 16) = "hello world!"; // 数组最后一个空间
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 18:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表