#include "stdio.h"
unsigned char p[10]={1,2,3,0,4,0};
int main ()
{
char m=0,i=0;
char temp =0;
m =sizeof(p);
temp = strlen(p);
printf("m=%d\r\n",m);
printf("temp=%d\r\n",temp);
for(i=0;i<10;i++)
{
printf("p=%x\r\n",p[i]);
}
while(1);
}
输出: m=10
temp = 3
sizeof():是计算数组长度大小 或字符串大小(+1包含 '/0') 或 数据的所占字节大小 例如 sizeof(char) = 1
strlen():计算从数组中的第一个数据开始到 数据中出现为0为止的数据长度大小。
或实际字符串的长度
#include "stdio.h"
unsigned char p[]="hello";
int main ()
{
char m=0,i=0;
char temp =0;
m =sizeof(p);
temp = strlen(p);
printf("m=%d\r\n",m);
printf("temp=%d\r\n",temp);
while(1);
}
输出: m=6
temp = 5
对于指针:*p 中P是指地址,对于32bit的MCU来说,P是32 位,四个字节
#include "stdio.h"
unsigned char *p="hello";
int main ()
{
char m=0,i=0;
char temp =0;
m =sizeof(p);
temp = strlen(p);
printf("m=%d\r\n",m);
printf("temp=%d\r\n",temp);
while(1);
}
输出;m = 4
temp = 5