char 指针如何判断字符串需要输出长度
先上代码:
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 }
运行结果:
[zyc@localhost ~]$ ./a.out ip:ABCDEFG ip size:8 g_ip:123456789 g_ip size:32
看8、9行代码,为什么ip是一个char指针, 但是%s的时候却知道到底要printf多长???