鱼C论坛

 找回密码
 立即注册
查看: 1481|回复: 11

[已解决]如何获取二维数组的长度

[复制链接]
发表于 2022-2-5 15:39:35 | 显示全部楼层 |阅读模式
5鱼币
如何获取二维数组的长度
最佳答案
2022-2-5 15:39:36
  1. $ cat main.c
  2. #include <stdio.h>

  3. int d0[10][20];

  4. void func(int x0[20][30]) {
  5.     printf("x0: %lu\n", sizeof(x0));
  6. }

  7. int main(void) {
  8.     int d1[20][30] = {0};
  9.     printf("d0: %lu\n", sizeof(d0));
  10.     printf("d1: %lu\n", sizeof(d1));
  11.     func(d1);
  12.     return 0;
  13. }
  14. $ gcc-debug -o main main.c
  15. main.c: In function ‘func’:
  16. main.c:6:31: warning: ‘sizeof’ on array function parameter ‘x0’ will return size of ‘int (*)[30]’ [-Wsizeof-array-argument]
  17.     6 |     printf("x0: %lu\n", sizeof(x0));
  18.       |                               ^
  19. main.c:5:15: note: declared here
  20.     5 | void func(int x0[20][30]) {
  21.       |           ~~~~^~~~~~~~~~
  22. $ ./main
  23. d0: 800
  24. d1: 2400
  25. x0: 8
  26. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-5 15:39:36 | 显示全部楼层    本楼为最佳答案   
  1. $ cat main.c
  2. #include <stdio.h>

  3. int d0[10][20];

  4. void func(int x0[20][30]) {
  5.     printf("x0: %lu\n", sizeof(x0));
  6. }

  7. int main(void) {
  8.     int d1[20][30] = {0};
  9.     printf("d0: %lu\n", sizeof(d0));
  10.     printf("d1: %lu\n", sizeof(d1));
  11.     func(d1);
  12.     return 0;
  13. }
  14. $ gcc-debug -o main main.c
  15. main.c: In function ‘func’:
  16. main.c:6:31: warning: ‘sizeof’ on array function parameter ‘x0’ will return size of ‘int (*)[30]’ [-Wsizeof-array-argument]
  17.     6 |     printf("x0: %lu\n", sizeof(x0));
  18.       |                               ^
  19. main.c:5:15: note: declared here
  20.     5 | void func(int x0[20][30]) {
  21.       |           ~~~~^~~~~~~~~~
  22. $ ./main
  23. d0: 800
  24. d1: 2400
  25. x0: 8
  26. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-5 15:49:18 | 显示全部楼层
如果没有作为参数传递过的话,可以用 sizeof

  1. $ cat main.c
  2. #include <stdio.h>

  3. int d0[10];

  4. void func(int x0[20]) {
  5.     printf("x0: %lu\n", sizeof(x0) / sizeof(x0[0]));
  6. }

  7. int main(void) {
  8.     int d1[20] = {0};
  9.     printf("d0: %lu\n", sizeof(d0) / sizeof(d0[0]));
  10.     printf("d1: %lu\n", sizeof(d1) / sizeof(d1[0]));
  11.     func(d1);
  12.     return 0;
  13. }
  14. $ gcc-debug -o main main.c
  15. main.c: In function ‘func’:
  16. main.c:6:31: warning: ‘sizeof’ on array function parameter ‘x0’ will return size of ‘int *’ [-Wsizeof-array-argument]
  17.     6 |     printf("x0: %lu\n", sizeof(x0) / sizeof(x0[0]));
  18.       |                               ^
  19. main.c:5:15: note: declared here
  20.     5 | void func(int x0[20]) {
  21.       |           ~~~~^~~~~~
  22. $ ./main
  23. d0: 10
  24. d1: 20
  25. x0: 2
  26. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-2-5 16:01:03 | 显示全部楼层

不能使用strlen函数吗
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-5 16:05:39 | 显示全部楼层
摸你穷 发表于 2022-2-5 16:01
不能使用strlen函数吗

strlen 函数是做什么用的?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-5 16:07:25 | 显示全部楼层
摸你穷 发表于 2022-2-5 16:01
不能使用strlen函数吗

求数组的长度?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-5 16:09:29 | 显示全部楼层
是求字符串的长度吧
字符串的长度可不等于数组的长度
  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <string.h>

  4. int main(void) {
  5.     char str[100] = "hello";
  6.     printf("%lu\n", strlen(str));
  7.     return 0;
  8. }
  9. $ gcc-debug -o main main.c
  10. $ ./main
  11. 5
  12. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-2-5 16:09:31 | 显示全部楼层
人造人 发表于 2022-2-5 16:05
strlen 函数是做什么用的?

哦哦哦了二位数组输入的是字符串怎么整
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-5 16:10:05 | 显示全部楼层
摸你穷 发表于 2022-2-5 16:09
哦哦哦了二位数组输入的是字符串怎么整

二维数组输入字符串?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-5 16:12:41 | 显示全部楼层
  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <string.h>

  4. int main(void) {
  5.     char *str[100] = {
  6.         "hello",
  7.         "word",
  8.         "1234abcd",
  9.         NULL
  10.     };
  11.     for(size_t i = 0; str[i]; ++i) {
  12.         printf("%.2lu: %s\n", strlen(str[i]), str[i]);
  13.     }
  14.     return 0;
  15. }
  16. $ gcc-debug -o main main.c
  17. $ ./main
  18. 05: hello
  19. 04: word
  20. 08: 1234abcd
  21. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-2-5 16:13:09 | 显示全部楼层
人造人 发表于 2022-2-5 16:10
二维数组输入字符串?

哦    直接strlen(变量名)就可以了懂了懂了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-5 16:14:32 | 显示全部楼层
  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <string.h>

  4. int main(void) {
  5.     char str[3][100] = {
  6.         "hello",
  7.         "word",
  8.         "1234abcd",
  9.     };
  10.     for(size_t i = 0; i < 3; ++i) {
  11.         printf("%.2lu: %s\n", strlen(str[i]), str[i]);
  12.     }
  13.     return 0;
  14. }
  15. $ gcc-debug -o main main.c
  16. $ ./main
  17. 05: hello
  18. 04: word
  19. 08: 1234abcd
  20. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 02:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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