鱼C论坛

 找回密码
 立即注册
查看: 1463|回复: 3

在main里面sizeof(s1)-sizeof(s2)=2,但是在自己写的函数里却等于0;

[复制链接]
发表于 2022-1-10 10:55:13 | 显示全部楼层 |阅读模式

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

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

x
在main里面sizeof(s1)-sizeof(s2)=2,但是在自己写的函数里却等于0;
  1. #include<stdio.h>
  2. int my_strcmp(char s1[], char s2[]);
  3. int main()
  4. {
  5.         char s1[] = "hello world";
  6.         char s2[] = "hello abc";

  7. printf("%d %d\n",sizeof(s1),sizeof(s2));
  8.         int flag = my_strcmp(s1, s2);
  9.        
  10.         printf("%d\n", flag);
  11.         printf("%d\n", sizeof(s1)-sizeof(s2));
  12.        
  13.         if(flag == 0)
  14.         {
  15.                 printf("%s 等于 %s\n", s1, s2);
  16.         }
  17.         else if(flag > 0)
  18.         {
  19.                 printf("%s 大于 %s\n", s1, s2);
  20.         }
  21.         else
  22.         {
  23.                 printf("%s 小于 %s\n", s1, s2);
  24.         }

  25.         return 0;
  26. }
复制代码
  1. #include<stdio.h>

  2. int my_strcmp(char s1[], char s2[])
  3. {
  4.         int c;
  5.         c= sizeof(s1)-sizeof(s2);
  6.         printf("%d\n", sizeof(s1)-sizeof(s2));
  7.         printf("%d\n",c);
  8. //        return sizeof(s1)-sizeof(s2);
  9.         return c;

  10. }
复制代码


打印结果为:
12 10
0
0
0
2
hello world 等于 hello abc

--------------------------------
这里不懂其原因,感谢帮助!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-1-10 11:46:08 | 显示全部楼层
数组作为参数传递给其他函数以后就不是数组了,变成指针了
  1. $ cat main.c
  2. #include <stdio.h>

  3. int a[10];

  4. void func(int b[20]) {
  5.     int c[30];
  6.     printf("sizeof(b) == %lu\n", sizeof(b));
  7.     printf("sizeof(a) == %lu\n", sizeof(a));
  8.     printf("sizeof(c) == %lu\n", sizeof(c));
  9. }

  10. int main(void) {
  11.     int d[20];
  12.     int e[40];
  13.     func(d);
  14.     printf("sizeof(a) == %lu\n", sizeof(a));
  15.     printf("sizeof(d) == %lu\n", sizeof(d));
  16.     printf("sizeof(e) == %lu\n", sizeof(e));
  17.     return 0;
  18. }
  19. $ gcc -g -Wall -o main main.c
  20. main.c: In function ‘func’:
  21. main.c:7:40: warning: ‘sizeof’ on array function parameter ‘b’ will return size of ‘int *’ [-Wsizeof-array-argument]
  22.     7 |     printf("sizeof(b) == %lu\n", sizeof(b));
  23.       |                                        ^
  24. main.c:5:15: note: declared here
  25.     5 | void func(int b[20]) {
  26.       |           ~~~~^~~~~
  27. $ ./main
  28. sizeof(b) == 8
  29. sizeof(a) == 40
  30. sizeof(c) == 120
  31. sizeof(a) == 40
  32. sizeof(d) == 80
  33. sizeof(e) == 160
  34. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-10 14:50:00 | 显示全部楼层
人造人 发表于 2022-1-10 11:46
数组作为参数传递给其他函数以后就不是数组了,变成指针了

好吧,感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-10 21:04:50 | 显示全部楼层
  1. #include <stdio.h>

  2. int check(const char *A, const char *B){
  3.     for(int i = 0; *(A+i) || *(B+i); i++) if(*(A+i) - *(B+i) != 0) return *(A+i) - *(B+i);
  4.     return 0;
  5. }

  6. int main(){
  7.     char *str1 = "hello world";
  8.     char *str2 = "hello abc";
  9.     printf(check(str1, str2) > 0 ? "%s > %s" : check(str1, str2) < 0 ? "%s < %s" : "%s == %s", str1, str2);
  10.     return 0;
  11. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 03:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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