我正在使用大小为4的char [],但是当我使用memcpy()函数时,它会在其中存储8个字符,并且字符数组长度也变为8.什么是happing?
我不想使用malloc ok.
char strRoh[4]={'\0'};
Run Code Online (Sandbox Code Playgroud)
然后
memcpy(strRoh,Dump+22,4);
Run Code Online (Sandbox Code Playgroud)
现在告诉我这有什么不对
char strIP[]="hhhhhhhh";
char strRoh[4]={'\0'};
char strTheta[4]={'\0'};
char strTimeStamp[6]={'\0'};
char strNMDump[48]={'\0'};
Run Code Online (Sandbox Code Playgroud)
是否有任何减速问题导致当我改变顺序他们的字符串也改变那里大小现在strroh得到10个字符
这到底是怎么回事
C字符串以0结尾.这意味着如果你想要一个nC 长度的字符串,你需要n+1 char它:
char hello[5] = "hello";
Run Code Online (Sandbox Code Playgroud)
不是一个字符串,因为它hello有5 char秒的空间,并且它不会以0.
char hello[6] = "hello";
Run Code Online (Sandbox Code Playgroud)
是一个字符串,并且具有6个字符:h,e,l,l,o,0.
为了能够在C中使用字符串相关的函数,您需要终止0.
因此,将代码更改为:
char strRoh[5]={'\0'};
char strTheta[5]={'\0'};
char strTimeStamp[7]={'\0'};
char strNMDump[49]={'\0'};
Run Code Online (Sandbox Code Playgroud)
请注意,在C中,当您执行以下操作时:
char hello[] = "hello";
Run Code Online (Sandbox Code Playgroud)
编译器为你做计数,并创建hello一个大小为6的数组(一个终止0):
printf("%zu\n", sizeof hello);
Run Code Online (Sandbox Code Playgroud)
将打印6.