sizeof测类型(数组名除外) strlen测实际长度 strncpy返回指针类型
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 int main() 5 { 6 char *p="wangddd"; 7 printf("%d\n",sizeof(p));//输出4,指针类型 8 9 char x[8]; 10 printf("%d\n",sizeof(x));//输出8,所占内存数 11 12 printf("%s\n",strncpy(x,"wangddddd",sizeof(x)));//返回x指针,正常显示前7位 13 //strncpy返回值是一个指针x 14 printf("%d\n",sizeof(strncpy(x,"wangddddd",sizeof(x))));//输出4 15 printf("%d\n",strlen(strncpy(x,"jskjksjdf",sizeof(x))));//随机 取决于编译环境 16 printf("%d\n",strlen(strncpy(x,"jskjkse",sizeof(x))));//输出7 17 18 char y[5]="w c"; 19 printf("%d\n",strlen(y));//输出3 20 printf("%d\n",sizeof(y));//输出5 21 22 return 0; 23 }
时间: 2024-10-08 23:43:38