先上代码:
1 #include <stdio.h> 2 #include <string.h> 3 4 5 const char g_ip[32] = "123456789"; 6 int func1(const char *ip) 7 { 8 printf("ip:%s\n",ip); 9 printf("ip size:%d\n",sizeof(ip)); 10 if(*ip == ‘\0‘) 11 printf("ip is none\n"); 12 printf("g_ip:%s\n",g_ip); 13 printf("g_ip size:%d\n",sizeof(g_ip)); 14 if(*ip == ‘\0‘) 15 printf("g_ip is none\n"); 16 return 0; 17 } 18 19 int main() 20 { 21 const char ip[32]="ABCDEFG"; 22 func1(ip); 23 return 0; 24 }
运行结果:
[[email protected] ~]$ ./a.out ip:ABCDEFG ip size:8 g_ip:123456789 g_ip size:32
看8、9行代码,为什么ip是一个char指针, 但是%s的时候却知道到底要printf多长???
原文地址:https://www.cnblogs.com/fallenmoon/p/8979683.html
时间: 2024-10-31 19:53:23